这是简单的装饰带参数:如何当一个参数传递装饰与装饰的功能或者说条件匹配
app.py
from __future__ import print_function
import time
def newdecorator(arg1):
def benchmarking(funct):
#The argument is accessible here
print("this is the value of argument",arg1)
def timercheck(*args, **kwarg):
starttime=time.time()
print("starting time",time.time())
funct(*args, **kwarg)
print("TOTAL TIME TAKEN ::",time.time()-starttime)
return timercheck
return benchmarking
#passing value to the decorators with arguments
@newdecorator('arg value')
def tara():
print("hellow testing")
if __name__=="__main__":
tara()
的装饰工作正常。它用一种功能来装饰功能,以显示其开始时间。
我想达到的目标: 我希望修饰者能有条件地实现我的意思。
-
当用户运行app.py让装修不执行
-
用户运行app.py -t比想要的装饰中实现
我将使用参数解析argparse模块,这不是一个大的p我想知道的是如何使装饰条件有条件地工作。