decorator one

One: function name resolution

First, we define a function and print their function and variable names respectively

def func():
    print(111)
print(func())
print(func)
print(())

print result:

Interpretation: First print 111 because the function func() is executed.

           The second print is empty, none, because the function has no return value.

           The memory address of the function name is printed this time, because the defining function itself is a variable.

           The last print() is because there is nothing in it, print what you put

 

2 The function name can be assigned a value.

def func():
    print(111)
def func1():
    print(222)
def func2():
    print(333)
f=func
x=func1
y=func2
f()
x()
and()
l1=[f(),x(),y()]
for i in l1:
    print(i)

print result:

Interpretation:

The first time it prints 111, 222, 333 because the function call result

The second time this result is printed is because the for loop opens this function, which is called separately above.

The third print is none because the function has no return value. Printing this function is none.

 

Function Name Exercise 2

def func2():
    print(111)
def func3(x):
    print(222)
    x()
func3(func2)

Execution result: 222,111

Interpretation: func3(fuc2) passes the value of func2 to x, prints 222, x() executes the above func2 function, and prints 111

 

Exercise 3

def func2():
    print(222)
def func3(x):
    print(a)  # func2()
    return a
a = 3
ret = func3 (a)
print (ret)

Print result: 3, 3

Analysis: The func2 function is not passed, so 222 is not executed

def func2():
    print(222)
def func3(x):
    # print(x)  # func2()
    return x
f = func2
f()
ret = func3 (func2) # x func2
right()
func3(func2)()

Print result: 222,222,222

The first prints 222, and the f() call is executed once.

The second print return x gives fun3(fun2) and x=func2 ret receives this x ret() is print222

The third time func3(func2)() is equivalent to x(), that is, it prints 222 again

 

Two: Closure function

An inner function references a variable of an outer function (non-global variable)

def  func():
    x=1
    def func1():
         print(x)
    func1()
    print( func1.__closure__)
func()

How to test if it is a closure function

The function name.__closure__, and then print it out containing cell is the closure

The following figure is not a closure, because it is a global variable, the print result is 1, NONE, none means it is not a closure.

closure trap

# Inside the outer function, execute inner()
# return inner
def wrapper():
    def inner():
        print(666)
    return inner
# wrapper()  # inner
wrapper()()

What is the execution process?

First open up a temporary space in the memory to store the wrapper function in it, then wrapper() starts to execute the function, returns inner, and finally, at this time inner=wrapper(), add () after it, start executing the inner function, print 666, This is the end of it.

Why is it not a closure, because there is no value passed, the return value shows none, and the internal value does not call the external value, what closure is it talking about.

def wrapper():
    def i():
        def inner():
            print(666)
        print(inner.__closure__)
        return inner
    return i
# wrapper()  # inner
wrapper()()

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325768772&siteId=291194637