def outer(origin):
def inner(*args, **kwargs):
print("before 110")
res = origin(*args, **kwargs) # 调用原来的func函数
print("after")
return resreturn inner
@outer # func1 = outer(func1)
def func1(a1):
print("我是func1函数")
value = (11, 22, 33, 44)
return value
应用:
from flask import Flask
app = Flask(__name__)
def auth(func):
def inner(*args, **kwargs):
# 在此处,判断如果用户是否已经登录,已登录则继续往下,未登录则自动跳转到登录页面。
return func(*args, **kwargs)return inner
@auth
def index():
return "首页"
重要补充:functools
def auth(func):
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner@auth
def handler():
passhandler()
print(handler.__name__) # inner
import functools
def auth(func):
@functools.wraps(func) # 应用
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner@auth
def handler():
passhandler()
print(handler.__name__) # handler