def outer(f):
def inner():
f()
print("新增功能1")
return inner
def wrap(f):
def inner():
f()
print("新增功能2")
return inner
?
def check_usr(fn): # fn, login, inner:不同状态下的login,所以参数是统一的
def inner(usr, pwd):
# 在原功能上添加新功能
if not (len(usr) >= 3 and usr.isalpha()):
print(‘账号验证失败‘)
return False
# 原有功能
result = fn(usr, pwd)
# 在原功能下添加新功能
# ...
return result
return inner
?
?
# 了解
def outer(input_color):
def wrap(fn):
if input_color == ‘red‘:
info = ‘\033[36;41mnew action\33[0m‘
else:
info = ‘yellow:new action‘
?
def inner(*args, **kwargs):
pass
result = fn(*args, **kwargs)
print(info)
return result
return inner
return wrap # outer(color) => wrap
?
?
color = input(‘color: ‘)
原文:https://www.cnblogs.com/guanchao/p/10644490.html