""" Requirements: 0 argument to show hope time with integer, floating point number 1 argument to display """ import time DEF get_run_time (Flag): "" " decorator factory function " "" DEF GET_TIME (FUNC): "" " decorator function: The function of time statistical " "" Print ( ' in GET_TIME ' ) def inner(*args, **kwargs): t1 = time.time() res = func(*args, **kwargs) T2 = the time.time () IF In Flag == 0: Print ( ' run DS% ' % (T2 - T1)) the else : Print ( ' run FS% ' % (T2 - T1)) return RES return inner return get_time # Decorative role factory function: # 1> decorator receiver function but can not directly accept the parameters required ----> reception parameter # 2> production of decorative object ----> generating function decorator # Association: a factory interior decorator function # real implementation procedure: # 1.> GET_TIME = get_run_time (parameter) # 2.> @get_time for decorating function func1 GET_TIME = func1 (func1) @get_run_time (0) # This otherwise the return value of the function name get_time DEF func1 (NUM, Age = 18 is ): for I in Range (. 3 ): time.sleep(1) print('in func', num, age) # @Get_run_time (1) Note: This should be seen separately f1 = get_run_time (1) This is a whole, function call, and then returns a value of # @ f1 This is the real beginning of the decorator function func1 ( 89 ) """ problem: 1.> decorator factory function is a decorator function? Instead, the interior decorator factory definition function, and return a reference function decorator 2.> relationship decorator factory functions and decorators? The return value is a reference to the factory decorator function. Its role is to create an object of a decorator function <address or call references more appropriate> """
A plurality of decorator decorative function:
""" <I> italic <B> bold, bold Multiple decorators: Extended multiple functions at the same time """ def makeBold(func): """加粗""" def inner(*args,**kwargs): return '<b>' + func() + '</b>' return inner def makeItalic(func): """倾斜""" def inner(*args,**kwargs): return '<i>' + func() + '</i>' return inner @makeItalic @makeBold @makeItalic DEF f1 (): return ' Life is short, I used Python ' Print (f1 ()) # <i> <b> <i> Life is short, I used Python </ i> </ b > </ i> # After the execution order, the first inclination, after bold, wear similar clothes, underwear first, jackets # Soul appreciated Code: f1 = makeItalic (f1) # 1.> F11 = makeItalic (F1) is makeItalic F11 inside inner; # 2.> F111 = makeBold (F11) F111 is inside the inner makeBold # Step in understanding: f1 = makeBold (makeItalic (f1 )) ------> simple understanding is a multi-layered decorative # like to send express themselves after one package courier brother will help you go a layer package . # Execution order must be executed innermost go. <Innermost decorator to complete decoration> # but it is the outermost layer of the first to begin decorating, decorate it needs to complete in order to decorate the inner layer.