# # Generator
-The generator is an object used to create a python sequence
-Usually the generator is the one that produces the data for the iterator
-for example, the range () function is a generator
-Each time the generator is iterated, it records the location of the last call and returns the next value, which makes it unnecessary for the program to create and store the complete sequence
# # Generator function
-The generator function is similar to a normal function, but its return value uses the yield statement instead of return
1 defMy_Range (start=0, last=10, step=1):2Number =Start3 whileNumber <Last :4 yield Number5Number + =Step6 7My_Range#is a normal function8 #<function My_Range at 0x7efe3dbf2e18>9 TenMy_Range ()#returns a Generator object One #<generator Object My_Range at 0x7efe3daac360> A -List (My_Range (1, 10)) - #[1, 2, 3, 4, 5, 6, 7, 8, 9]
# # Decorator
-The function of the adorner is to supplement the functionality of the original code without changing the original code structure.
-The adorner is essentially a higher-order function that takes a function as a parameter, which enters a function as a parameter and returns a new function that expands function
1 #Adorner function, add two statements to the function2 defDeco (FN):3 defNew_func (*args):#the parameters of the intrinsic function need to be the same as the parameters of the incoming FN4 Print("Execute function: {0}". Format (FN.__name__))5result = FN (*args)6 Print("function Execution Result: {0}". Format (result))7 returnresult8 returnNew_func9 Ten One@deco#with the @ DECORATION function name, after using the adorner, add actually points to the new function returned by the doco function A defAdd (*args): - Print("I am the core code, can not change my") -result =0 the forNinchargs: -Result + =N - returnresult - + -Add (1, 2, 3, 4) + """ A Execution Result: at Execute function: Add - I am the core code, can not change my - function Execution Result: ten - """
-a function can have more than one adorner
-the adorner closest to the function executes first and then executes the adorner one at a time
1 defCount_param (FN):2 defNew_func (*args):3Amount =len (args)4FN (*args)5 Print("the number of arguments is: {0}". Format (amount))6 returnAmount7 returnNew_func8 9 Ten @count_param One @deco A defAdd (*args): - Print("I am the core code, can not change my") -result =0 the forNinchargs: -Result + =N - returnresult - + -Add (1, 2, 3, 4) + """ A Execution Result: at Execute function: Add - I am the core code, can not change my - function Execution Result: ten - Number of parameters: 4 - """
-If the decorator itself needs to pass in parameters, then you need to write a higher-order function that returns decorator
1 Import Time2 3 4 deflog (now_time):5 defDeco (FN):6 defNew_func (*args, * *Kwargs):7 Print(now_time)8 returnFN (*args, * *Kwargs)9 returnNew_funcTen returndeco One A - @log (Time.asctime (Time.localtime (Time.time () )) - defAdd (*args): the Print("I am the core code, can not change my") -result =0 - forNinchargs: -Result + =N + returnresult - + AAdd (1, 2, 3, 4) at """ - Execution Result: - function Start time: Sun Jul 1 15:30:14 2018 - I am the core code, can not change my - """
-The __name__ property of the Add function is now printed to discover:
Print (" core function name: {0}". Format (Add. __name__) "" " output: core function Name: New_func" ""
-This shows that although the content of the core function is not changed on the adorner surface, it actually modifies the properties of the core function, so it is also necessary to copy the __name__ property of the core function to the new function
1 Import Time2 3 4 deflog (now_time):5 defDeco (FN):6 defNew_func (*args, * *Kwargs):7 #Copy the __name__ property of the original function to the new function8New_func.__name__= fn.__name__9 Print(now_time)Ten returnFN (*args, * *Kwargs) One returnNew_func A returndeco - - the @log (Time.asctime (Time.localtime (Time.time () )) - defAdd (*args): - Print("I am the core code, can not change my") -result =0 + forNinchargs: -Result + =N + returnresult A at -Add (1, 2, 3, 4) - Print("Core function name: {0}". Format (Add.__name__)) - """ - Execution Result: - Sun Jul 1 15:43:00 2018 in I am the core code, can not change my - Core Function Name: Add to """
-There is a special function in Functools to deal with this problem.
1 Import Time2 ImportFunctools3 4 5 deflog (now_time):6 defDeco (FN):7@functools. Wraps (FN)#add an adorner to the new function to modify the __name__ property of the new function8 defNew_func (*args, * *Kwargs):9 Print(now_time)Ten returnFN (*args, * *Kwargs) One returnNew_func A returndeco - - the @log (Time.asctime (Time.localtime (Time.time () )) - defAdd (*args): - Print("I am the core code, can not change my") -result =0 + forNinchargs: -Result + =N + returnresult A at -Add (1, 2, 3, 4) - Print("Core function name: {0}". Format (Add.__name__)) - "" " - Execution Result: -Sun Jul 1 15:48:10 2018 in I am the core code, can not change my - Core Function Name: Add to“”“
This article refers to:
Beauty Bill Lubanovic "Python language and its application"
Https://www.liaoxuefeng.com Liaoche's official website
Python builder, adorner