When assigning a function to a variable in python, you need to pay attention to some issues.
Preface
This article mainly introduces some issues that need to be paid attention to when python assigns a function to a variable. We will share these issues for your reference, let's take a look at the detailed introduction:
We have seen two types of functions assigned to variables. One is
a=f
The other is
a=f()
There are differences between the two forms.
1. Type a = f points a variable to a function.
Verify with code:
>>> f = abs>>> f(-10)10
It indicates that the variable f has now pointed to the abs function itself. Direct callabs()Functions and call Variablesf()Identical. This is an example of Liao Xuefeng's python tutorial.f()And callabs()Yes.
Another example of a factory function is as follows:
def maker(N): def action(X): return X**N return action
The Outer Return Value of this nested function is the function name of the inner function. Note that there are no parentheses. There is a large area of brackets here. Call the external function:
f=maker(2)
As mentioned above, f points to the action function with the condition N = 2. It can be understood as the action function when f is equal to N or 2. Let's call it:
>>> f(3)9
It proves that f and action functions are the same.
2. a = f () is the process of assigning the return value of f () to.
Here a only receivesf()Iff()If no value is returned, a is assigned as None. It is worth noting thata=f(),f()It will run once. This is what I just figured out, for example:
>>> def add(x,y): z=x+y print(z)>>>a=add(3,4)7
Although only one value assignment statement is executed, result 7 is output, indicating that the value assignment function add is executed. However, the value of a is None and can only be displayed through the print statement. Not only will the function be executed during the value assignment process, but will also be executed 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 a routine of the python course decorator section of Liao Xuefeng. At first I thoughtreturn func(*args,**kw)This statement returnsnow()The Return Value of the function (that is, the func function), and later found that the now function has no return value, that is, None, so this statement is actually in the value assignment process,
func(*args,**kw)The print statement of the function now is executed.
In the following exercise, a deformation requires that 'in in call' and 'end call' be printed before and after the function call. The program of the following netizen is written as follows:
def wrapper(*args,**kw): print(t+'begin call') result=func(*args,**kw) print(t+'end call') return result
I don't quite understand whyresult=func(*args,**kw)After understanding this sentence, I realized that the assignment itself is not meaningful, but this sentence makes the func function run at the same time, so I wrote it
def wrapper(*args,**kw): print(t+'begin call') func(*args,**kw) print(t+'end call')
The results are the same.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.