Closed function: closed function => define the function inside the function; the characteristic of closed function is that it can only be used inside the function
def outter():
x = 100
def wrapper():
print(x)
Package function: The function refers to a name, which is a
summary from the e level : a closure function refers to a function defined inside a function that refers to a name
closure function from the scope of an outer function Upgrade: Combine function objects
def outter():
x = 100
def wrapper():
print(x)
return wrapper # 千万不要加括号
f = outter()
print(f)
def foo():
x = 666
f()
foo()
Learning Closure Function Learned a new solution
for passing parameters to the function body. There are two solutions for passing parameters to the function body.
Solution 1: Pass it directly in the form of parameters
def wrapper(x):
print(x)
wrapper(100)
Option II:
def outter(x):
def wrapper():
print(x)
return wrapper # 千万不要加括号
f = outter(100)
f()
Application of Closure Function-Decorator
1. What is a decorator? A
decorator refers to a tool for decorating objects. Decoration refers to adding new functions to the decorated object.
However, the principle that must be followed to implement decorators is "open and closed" The principle of "
open " refers to being open to expanding new functions, and closed refers to being closed to modifying the source code and calling methods. The
overall decorator refers to the creation of a tool that can follow principles 1 and 2 Under the premise of, you can add new functions to the decorated object.
Principle 1. Do not modify the source code of the decorated object.
2. Do not modify the calling method of the decorated object.
2. Why do you use the decorator to
do this. If there is a problem with updating new functions, you can quickly stabilize
3. How to implement decorator
decorator----------function
decorated object-------function
Ready to be decorated
import time
def index():
print('from index')
time.sleep(3)
index()
Solution one changed the source code
import time
def index():
start = time.time()
print('from index')
time.sleep(3)
end = time.time()
print(f"run time is {end - start}")
index()
Scheme 2 The function of the decorator needs to be written repeatedly, resulting in code redundancy
import time
def index():
print('from index')
time.sleep(3)
start = time.time()
index()
end = time.time()
print(f"run time is {end - start}")
Scheme 3 The calling method has changed and the code has been written dead
import time
def index():
print('from index')
time.sleep(3)
def wrapper():
start = time.time()
index()
end = time.time()
print(f"run time is {end - start}")
wrapper()
Option Four
import time
def index():
print('from index')
time.sleep(3)
def outter(index):
def wrapper():
start = time.time()
index()
end = time.time()
print(f"run time is {end - start}")
return wrapper
index = outter(index)
index()
Option Five
import time
def index():
print('from index')
time.sleep(3)
def home(name):
print(f"welcome {name} to home page")
time.sleep(0.5)
def outter(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
end = time.time()
print(f"run time is {end - start}")
return (res)
return wrapper
home = outter(home)
index = outter(index)
res = home('holmes')
print(res)
index()
Decorator syntactic sugar
import time
def outter(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
end = time.time()
print(f"run time is {end - start}")
return (res)
return wrapper
@outter
def index():
print("from index")
time.sleep(3)
@outter
def home(name):
print(f"welcome {name} to xinjiang")
time.sleep(0.5)
res = home('holmes')
print(res)
index()
Decorator template
def deco(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
return wrapper
User login decorator
def auth(func):
def wrapper(*args, **kwargs):
inp_name = input('username:').strip()
inp_pwd = input('password:').strip()
if inp_name == 'lu' and inp_pwd == '345':
res = func(*args, **kwargs)
return res
else:
print('登陆失败')
return wrapper
@auth
def index():
print('from index')
time.sleep(3)
index()
Time decorator
import time
def timmer(func):
def wrapper1(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
stop = time.time()
print(stop - start)
return res
return wrapper1
Decorator application
import time
def timmer(func):
def wrapper1(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
stop = time.time()
print(stop - start)
return res
return wrapper1
def auth(func):
def wrapper2(*args, **kwargs):
inp_user = input('Username: ').strip()
inp_pwd = input('Password: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
res = func(*args, **kwargs)
return res
else:
print('登录失败')
return wrapper2
@auth
@timmer
def index():
print('====>index')
time.sleep(3)
index()