table of Contents
Closure function
What is the function of closure
A built-in function, a function of external variables referenced, the inline function was to become a closure function
Definition of the external function is called but variable internal reference function free variables
Closure nested function is a function of at least
def f1(x):
def f2():
print(x)
return f2
num = f1(2)
num()
def outer(x,y):
def inner():
nonlocal x ,y
x += 1
y += 2
print(f'x:{x},y:{y}')
return inner
test = outer(2,5)
test()
test()
Using the function to remove closures using global variables, the inner and outer function encapsulated in function, not external call.
When the run is complete outer function, its parameters x, y remain because they are the closures, it will not be recovered.
Decorator
Corresponding to the advanced application decorator closure function, transfer function parameters of the closure is a variable, while the decorator parameter is a transfer function. Decoration is not used to modify the source code and function calls way to add to the original function.
- Decorator itself is a function, but it used to be decorated with decorative function
- Decorative decorative function does not change the source code to be decorated function
- Decorator decorative function does not change the decorated function is called
import time
def index():
"""被装饰的函数"""
print('index')
time.sleep(1)
# time_count装饰器:对被装饰函数计时
def time_count(func): # func才是真正的index
"""装饰器"""
def wrapper():
start = time.time()
func()
end = time.time()
print(end - start)
return wrapper
index = time_count(index) # index == wrapper
index() # wrapper()
Return value of the decorator
import time
def index():
"""被装饰的函数"""
print('x',x)
print('index')
time.sleep(1)
return 'index'
# time_count装饰器:对被装饰函数计时
def time_count(func): # func才是真正的index
"""装饰器"""
def wrapper():
start = time.time()
res = func() # index()
end = time.time()
print(end - start)
return res
return wrapper
index = time_count(index) # index == wrapper
res = index() # wrapper()
print(res)
For the return value of the function, we can take a variable name to receive the return value
res = func () then return res
There are arguments decorator
import time
def index(x,y,z=10):
"""被装饰的函数"""
print('x',x)
print('index')
time.sleep(1)
return 'index'
# time_count装饰器:对被装饰函数计时
def time_count(func): # func才是真正的index
"""装饰器"""
def wrapper(*args,**kwargs): # (10, 20) # *args和**kwargs接收了所有的参数
start = time.time()
res = func(*args,**kwargs) # index() # *(10,20) # *args和**kwargs打散参数传给真正的index
end = time.time()
print(end - start)
return res
return wrapper
index = time_count(index) # index == wrapper
res = index(10,20,320) # wrapper()
print(res)
If we do not know how many of the original function parameters, we can use variable-length parameters decorator
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
Finally, re-transmission of values into use
Decorator syntax sugar
@ Decorator function name
index = time_count(index) == @time_count
Decorator function to write on top of the decorative function, @ write the following decorated function.
Decorative template
To sum up, we can get a template decorator, like when applied directly use
def deco(func):
def wrapper(*args,**kwargs):
res = func(*args,**kwargs)
pass
return res
return wrapper
@decp
def show(name, age):
print(name,age)
show('ly',10)