python装饰器
#通过引用全局变量来控制账号登陆
flag = False def login(func): def inner(*args,**kwargs): global flag if flag: ret = func(*args, **kwargs) return ret else: username = input('user:') password = input('pswd:') if username == 'ryan' and password == '123456': flag = True ret = func(*args, **kwargs) return ret return inner @login def say_hi(): print('hello world') @login def say_bye(): print('goodbye my friends') say_hi() say_bye()
计时器:
from time import time from time import sleep def timmer(func): def inner(*args,**kwargs): start = time() ret = func(*args,**kwargs) end = time() print(end - start) return ret return inner @timmer def func_1(words): sleep(1) print(words) @timmer def func_2(words): sleep(2) print(words) func_1('你好呀') func_2('你是大坏蛋')
posted on 2020-07-23 21:45 Hello_Thanos 阅读(92) 评论(0) 编辑 收藏 举报