Last active
April 30, 2022 13:46
-
-
Save neuromaancer/756f4650b4fd711acbd48bb715644220 to your computer and use it in GitHub Desktop.
python-decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 这是装饰器函数,参数 func 是被装饰的函数 | |
def logger(func): | |
def wrapper(*args, **kw): | |
print('主人,我准备开始执行:{} 函数了:'.format(func.__name__)) | |
# 真正执行的是这行。 | |
func(*args, **kw) | |
print('主人,我执行完啦。') | |
return wrapper | |
# 这是装饰函数 | |
def timer(func): | |
def wrapper(*args, **kw): | |
t1=time.time() | |
# 这是函数真正执行的地方 | |
func(*args, **kw) | |
t2=time.time() | |
# 计算下时长 | |
cost_time = t2-t1 | |
print("花费时间:{}秒".format(cost_time)) | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment