the decorator argument and the original functionand returns the callable oncallwhich ultimately invokes the original function on later calls because this structure creates new decorator and oncall functionstheir enclosing scopes are per-decoration state retention we can put this structure to use in our timer to allow label and trace control flag to be passed in at decoration time here' an example that does just thatcoded in module file named timerdeco py so it can be imported as general toolit uses class for the second state retention level instead of nested functionbut the net result is similarimport time def timer(label=''trace=true)on decorator argsretain args class timerdef __init__(selffunc)on @retain decorated func self func func self alltime def __call__(self*args**kargs)on callscall original start time clock(result self func(*args**kargselapsed time clock(start self alltime +elapsed if traceformat '% % fvalues (labelself func __name__elapsedself alltimeprint(format valuesreturn result return timer mostly all we've done here is embed the original timer class in an enclosing functionin order to create scope that retains the decorator arguments per deployment the outer timer function is called before decoration occursand it simply returns the timer class to serve as the actual decorator on decorationan instance of timer is made that remembers the decorated function itselfbut also has access to the decorator arguments in the enclosing function scope timing with decorator arguments this timerather than embedding self-test code in this filewe'll run the decorator in different file here' client of our timer decoratorthe module file testseqs pyapplying it to sequence iteration alternatives againimport sys from timerdeco import timer force list if sys version_info[ = else (lambda xx@timer(label='[ccc]==>'def listcomp( )return [ for in range( )like listcomp timer)(listcomplistcomptriggers timer __call__ coding function decorators