Four ways to call Python functions-basic focus

Source: Internet
Author: User

First: parameters are called sequentially from the first parameter to the back row # Standard

# -*-coding:utf-8-*-   def Normal_invoke (x, y):       Print " --normal_invoke:-- "      Print " x is%d " %x      print"y is%d"
#标准调用  Normal_invoke (1, 2)  

Operation Result:

--normal_invoke:--  is 1  is 2  

The second type:#关键字调用

def Keyword_invoke (x, y):       Print " --keyword_invoke--: "      Print " x is%d " % x      print"y is%d"

#关键字调用  
Note: Where the critical call starts, it must be called with the keyword in addition to the ' * ' collection parameters. Cannot be written like this: Keyword_invoke (Y=1, 2), this will be an error.

Operation Result:

--keyword_invoke--:  is 2  is 1

Third type:#非关键字可变长参数 (tuple) def normal_tuple_invoke (x, y=2, *values):

 print   "  --normal_tuple_invoke--  " print  Span style= "COLOR: #800000" > " x is%d  " % x  print  "  y is%d   "% y  for  value in   values:  print  "  tuple ' s value has   ", value 

#非关键字可变长参数 (tuple)   

Normal_tuple_invoke (1, 3, ' xyz ', 123.4)

Note: Collect parameters ' * ' When you are unsure how much you are using it, he will put the indeterminate part in the values tuple, of course the tuple name depends on what is followed by ' * '.

Operation Result:

--normal_tuple_invoke--   is 1   is 3  tuple' s value has xyz   Tuple's value has 123.4  

Type Fourth:#关键字可变长参数 (dict)

defKeyword_dict_invoke (x, y=2, * *value_dict):Print "--keyword_dict_invoke--"      Print "x is%d"%xPrint "y is%d"%y forKeyinchvalue_dict:Print "Key is", KeyPrint "value is", Value_dict[key]

#关键字可变长参数 (dict)

Keyword_dict_invoke (y=1, x=2, arg2=' def ', arg1=' put at the last ')

Note: The parameters returned by the **value_dict are stored in the form of a dictionary: Key-value, and the first sequence of strokes is: Key, followed by: value.

Operation Result:

--keyword_dict_invoke--   is 2  was 1is     Arg1  is  put  on the last was  arg2  is   def    Process finished with exit code 0  

Part V: Another is to pass the whole function as a parameter, and I now know that the adorner is in use

Adorner is actually to add functionality to a program, but the program has been online or has been used, then can not be large-scale modification of the source code, this is not scientific and unrealistic, because it produced an adorner, so that it satisfies:

    1. Cannot modify the source code of the decorated function
    2. Cannot modify the calling method of the decorated function
    3. Add functionality to programs that meet 1 or 2 of cases

It is our goal to meet the three-point principle on demand. Because, let's start with this three-point principle to understand the adorner.

Wait, I'm going to tell you before the requirements. The principle of adorners consists of:

< function + argument higher order function + return value higher order function + nested function + syntax sugar = adorner >

This formula is the soul of the decorator!

 improt time  def   Test (): Time.sleep ( 2)  print  ("  test is running!   " )  def   Deco ( Func): Start  = Time.time () func ()  #  2  stop = Time.time ()  print  (Stop-start) Deco (test)  #  1  

Let's take a look at this code, in # #, we pass the test as an argument to the parameter func, the Func=test. Note that the address is passed here, which is where Func also points to the function body defined by the previous test, which can be said to be inside the Deco (), and Func is the test. In the # # place, the function name is appended with parentheses, which is the call to the function (execute it). Therefore, the result of this code operation is:

is is 3.0009405612945557 

About adorners I have a detailed explanation in another article!

---a little progress every day!!!

Four ways to call Python functions-basic focus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.