Home  >  Article  >  Backend Development  >  Pay attention to the detailed explanation of the problems when assigning functions to variables in python

Pay attention to the detailed explanation of the problems when assigning functions to variables in python

黄舟
黄舟Original
2017-08-20 10:50:572104browse

Variable assignment is a problem we often encounter in daily development. The following article mainly introduces you to some issues that need to be paid attention to when assigning functions to variables in python. The article introduces it through example code. It is detailed and has certain reference and learning value for everyone. Friends who need it can take a look below.

Preface

This article mainly introduces you to some issues that need to be paid attention to when assigning functions to variables in python. I will share them with you. Reference study, not much to say below, let’s take a look at the detailed introduction:

I have seen two forms of function assignment to variables, one is


a=f

The other is


a=f()

There is a difference between these two forms, let’s summarize them respectively.

1.a=F type is used to point variables to functions.

Verify it with the code:


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

It means that the variable f now points to the abs function itself. Directly calling the abs() function is exactly the same as calling the variable f(). This is an example from Teacher Liao Xuefeng's python tutorial. Now calling f() is the same as calling abs().

Another example of a factory function:


def maker(N):

  def action(X):

    return X**N

  return action

The outer return value of this nested function is that of the inner function Function name, please note that there are no parentheses. There is a big difference whether there are parentheses or not. At this time, the external function is called:


f=maker(2)

As mentioned above, f points to the action function, and the restriction condition is N=2. It can be understood that f is N equal to 2 action function. Let’s call it:


>>> f(3)
9

Prove that f and action functions are the same.

2.a=f() type belongs to the process of assigning the return value of f() to a

a here only receives The return value of f(), if f() has no return value, then a is assigned the value None. One thing worth noting here is that during the execution of a=f(), f() will run once, which is what I just figured out, such as:


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

Although only one assignment statement is executed here, the result 7 is output, indicating that the assignment process function add is executed, but the value of a is None and can only be passed through the print statement can be displayed. Not only the assignment procedure function will be executed, but also the return statement written in it.


>>>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 routine in the decorator section of Teacher Liao Xuefeng’s python tutorial. At first I thought return func(*args,**kw) This statement returns the return value of the now() function (i.e. func function). Later, it was discovered that the now function has no return value, which is None, so this statement is actually in the assignment process,

func(*args,**kw) is executed, that is, the print statement of function now is executed.

In the following exercises, one variation is to print out 'begin call' and 'end call' before and after the function call. The program written by a netizen below is like this:


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

At first I didn’t quite understand why I used result=func(*args,**kw), but later I realized that the assignment itself has no meaning, just this sentence This also causes the func function to run, so writing


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

results in the same result.

Summarize

The above is the detailed content of Pay attention to the detailed explanation of the problems when assigning functions to variables in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn