Learn Python decorators in nine steps
This example describes the Python decorator. Share it with you for your reference. The specific analysis is as follows:
This is the content introduced in the Python Learning Group. It is a good way to learn and practice more.
Step 1: Prepare the simplest function and add additional functions.
?
1 2 3 4 5 6 |
#-*-Coding: gbk -*- '''Example 1: The simplest function, indicating two calls ''' Def myfunc (): Print ("myfunc () called .") Myfunc () Myfunc () |
Step 2: Use the decoration function to add additional functions before and after function execution
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#-*-Coding: gbk -*- ''' Example 2: replacement function (decoration) The parameters of the decoration function are the decorated function objects, and the original function objects are returned. Substantive statement for decoration: myfunc = deco (myfunc )''' Def deco (func ): Print ("before myfunc () called .") Func () Print ("after myfunc () called .") Return func Def myfunc (): Print ("myfunc () called .") Myfunc = deco (myfunc) Myfunc () Myfunc () |
Step 3: Use the syntax sugar @ to describe the function
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#-*-Coding: gbk -*- ''' Example 3: Use the syntax sugar @ to decorate the function, which is equivalent to "myfunc = deco (myfunc )" However, it is found that the new function is called only for the first time, and the original function calls '''' multiple times ''' Def deco (func ): Print ("before myfunc () called .") Func () Print ("after myfunc () called .") Return func @ Deco Def myfunc (): Print ("myfunc () called .") Myfunc () Myfunc () |
Step 4: Use nested packaging functions to ensure that each new function is called
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#-*-Coding: gbk -*- '''Example 4: Use nested packaging functions to ensure that each new function is called, The parameters and return values of nested function are the same as those of the original function, The decoration function returns the nested packaging function object ''' Def deco (func ): Def _ deco (): Print ("before myfunc () called .") Func () Print ("after myfunc () called .") # If you do not need to return func, you should actually return the original function's return value. Return _ deco @ Deco Def myfunc (): Print ("myfunc () called .") Return 'OK' Myfunc () Myfunc () |
Step 5: Describe the functions with Parameters
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#-*-Coding: gbk -*- ''' Example 5: decorate a function with parameters, The parameters and return values of nested function are the same as those of the original function, The decoration function returns the nested packaging function object ''' Def deco (func ): Def _ deco (a, B ): Print ("before myfunc () called .") Ret = func (a, B) Print ("after myfunc () called. result: % s" % ret) Return ret Return _ deco @ Deco Def myfunc (a, B ): Print ("myfunc (% s, % s) called." % (a, B )) Return a + B Myfunc (1, 2) Myfunc (3, 4) |
Step 6: Describe functions with uncertain number of parameters
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#-*-Coding: gbk -*- '''Example 6: decorate a function with an uncertain number of parameters, Parameters (* args, ** kwargs) are used to automatically adapt to variable parameters and named parameters ''' Def deco (func ): Def _ deco (* args, ** kwargs ): Print ("before % s called." % func. _ name __) Ret = func (* args, ** kwargs) Print ("after % s called. result: % s" % (func. _ name __, ret )) Return ret Return _ deco @ Deco Def myfunc (a, B ): Print ("myfunc (% s, % s) called." % (a, B )) Return a + B @ Deco Def myfunc2 (a, B, c ): Print ("myfunc2 (% s, % s, % s) called." % (a, B, c )) Return a + B + c Myfunc (1, 2) Myfunc (3, 4) Myfunc2 (1, 2, 3) Myfunc2 (3, 4, 5) |
Step 7: Bring parameters to the decorator
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#-*-Coding: gbk -*- ''' Example 7: Based on example 4, let the decorator carry parameters, Compared with the previous example, a layer of packaging is added to the outer layer. The decoration function name should actually be more meaningful ''' Def deco (arg ): Def _ deco (func ): Def _ deco (): Print ("before % s called [% s]." % (func. _ name __, arg )) Func () Print ("after % s called [% s]." % (func. _ name __, arg )) Return _ deco Return _ deco @ Deco ("mymodule ") Def myfunc (): Print ("myfunc () called .") @ Deco ("module2 ") Def myfunc2 (): Print ("myfunc2 () called .") Myfunc () Myfunc2 () |
Step 8: Let the ornament carry Class Parameters
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#-*-Coding: gbk -*- '''Example 8: The decorator has class parameters ''' Class locker: Def _ init _ (self ): Print ("locker. _ init _ () shocould be not called .") @ Staticmethod Def acquire (): Print ("locker. acquire () called. (This is a static method )") @ Staticmethod Def release (): Print ("locker. release () called. (object instance not required )") Def deco (cls ): ''' Cls must implement the acquire and release static methods ''' Def _ deco (func ): Def _ deco (): Print ("before % s called [% s]." % (func. _ name __, cls )) Cls. acquire () Try: Return func () Finally: Cls. release () Return _ deco Return _ deco @ Deco (locker) Def myfunc (): Print ("myfunc () called .") Myfunc () Myfunc () |
Step 9: decorator includes class parameters and splits public classes into other py files. It also demonstrates how to apply multiple decorator to a function.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#-*-Coding: gbk -*- '''Mylocker. py: public class for example 9. py ''' Class mylocker: Def _ init _ (self ): Print ("mylocker. _ init _ () called .") @ Staticmethod Def acquire (): Print ("mylocker. acquire () called .") @ Staticmethod Def unlock (): Print ("mylocker. unlock () called .") Class lockerex (mylocker ): @ Staticmethod Def acquire (): Print ("lockerex. acquire () called .") @ Staticmethod Def unlock (): Print ("lockerex. unlock () called .") Def lockhelper (cls ): ''' Cls must implement the acquire and release static methods ''' Def _ deco (func ): Def _ deco (* args, ** kwargs ): Print ("before % s called." % func. _ name __) Cls. acquire () Try: Return func (* args, ** kwargs) Finally: Cls. unlock () Return _ deco Return _ deco |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#-*-Coding: gbk -*- ''' Example 9: the decorator carries class parameters and splits public classes into other py files. It also demonstrates how to apply multiple decorators ''' to a function ''' From mylocker import * Class example: @ Lockhelper (mylocker) Def myfunc (self ): Print ("myfunc () called .") @ Lockhelper (mylocker) @ Lockhelper (lockerex) Def myfunc2 (self, a, B ): Print ("myfunc2 () called .") Return a + B If _ name __= = "_ main __": A = example () A. myfunc () Print (a. myfunc ()) Print (a. myfunc2 (1, 2 )) Print (a. myfunc2 (3, 4 )) |
I hope this article will help you with Python programming.