from time import time def fa(fun): def fb(arg): print('start2') start = time() result = fun(arg) print(time() - start) print('end2') return result return fb def fc(fun): def fb(arg): print('start1') result = fun(arg) print('end1') return result return fb @fc @fa def code(arg): print('code haha') return arg + '__zxl' # code=fa(code) print(code('hello'))
输出:
start1
start2
code haha
0.0
end2
end1
hello__zxl
from time import time def fz(flag=True): def fa(fun): def fb(*args): print('打印日志') if flag else print('不打印日志') print('start2') start = time() result = fun(*args) print(time() - start) print('end2') return result return fb return fa @fz(False) # @fz(True)
# @fz()
def code(*args): print(args) return sum(args) print(code(3, 4, 5, 6, 7))
输出:
不打印日志
start2
(3, 4, 5, 6, 7)
0.0
end2
25
is_login = False def login(type='weixin'): def login1(fun): def code(): global is_login if not is_login: print(type) username = input('用户名:') password = input('密码:') if username == 'zxl' and password == 'zxl': print('登录成功') is_login = True fun() else: print('登陆失败') else: fun() return code return login1 @login() def home(): print('主页') @login('QQ') def news(): print('新闻') home() news()