#python # Some problems when assigning a function to a variable

Source: Internet
Author: User

Have seen two functions assigned to a variable form, one is

A=f

The other is

A=f ()

These two forms are differentiated and summarized separately.

the 1.a=f type belongs to the Variable-pointing function .

Verify with Code:

>>> f = abs>>> f(-10)10

The description variable f is now pointing to the abs function itself. The direct call abs() function is exactly the same as the calling variable f() . This is the example of a Python tutorial by Liu xuefeng, now calling F () and calling Abs () is the Same.

Another example of a factory function:

def maker (N):

def Action (X):

Return X**n

Return action

This nested Function's outer return value is the function name of the inner function, note that there is no parenthesis, there is a big difference between the Parentheses. This calls the external function:

F=maker (2)

As described above, F points to the action function, and the constraint is n=2, which can be understood as an action function with an F of n equals 2 o'clock. Let's call It:

>>> f(3)9

Prove that F is the same as the action Function.

The 2.a=f () type is a process that assigns the return value of F () to a

Here a only receives the return value of F (), if F () has no return value, then A is assigned to NONE. One thing to note here is that, during the execution of A=f (), F () runs once, which I just figured out, such as:

>>> def add(x,y):
    z=x+y
    print(z)
>>>a=add(3,4)
7

Although there is only one assignment statement executed, it outputs the result 7, stating that the assignment procedure function add executes, whereas the value of a is none and can only be displayed through the print Statement. Not only is the assignment process function executed, It is also written in the return Statement.

>>>def log(func):     def wrapper(*args, **kw):    print(‘call %s():‘ % func.__name__)    return func(*args, **kw)   return wrapper
>>>@log>>>def now():    print(‘2015-3-25‘)
 

This is the example of the section of the Python tutorial decorator of Liao Xuefeng teacher, at first I thought return func (*args,**kw) This statement returned the return value of the now () function (that is, the func Function) and later found that the now function has no return value, that is, none, So actually this is the statement in the assignment process,

Func (*args,**kw) executes, that is, the print statement for function now Executes.

In the following exercise, a variant is required to print ' Begin call ' and ' end calls ' before and after the function is called, and the following User's program Reads:

def wrapper(*args,**kw):            print(t+‘begin call‘)            result=func(*args,**kw)            print(t+‘end call‘)            return result

began to understand why the use of Result=func (*args,**kw) this sentence, later understand the fact that the assignment itself is not meaningful, but this sentence at the same time make the Func function run, so write

def wrapper(*args,**kw):            print(t+‘begin call‘)            func(*args,**kw)            print(t+‘end call‘)

The result is the Same.

 
 
 
   
 

#python # Some problems when assigning a function to a variable

Related Article

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.