Nine Steps learn python decorator

Source: Internet
Author: User
Tags split wrapper python decorator

This example describes the Python adorner. Share to everyone for your reference. The specific analysis is as follows:

This is the content which is introduced in the Python Study group, now learns to sell, the practice is the good study way.

The first step: The simplest function to prepare additional features

?

1 2 3 4 5 6 #-*-CODING:GBK-*-' Example 1: The simplest function, which means two times ' Def myfunc (): Print ("MyFunc () called.") MyFunc () MyFunc ()

Step two: Use the decoration function to attach additional functionality before and after the function is executed

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 #-*-CODING:GBK-*-' Example 2: Replace function (decoration) The parameters of the decoration function are decorated function objects, return the original function object decoration of the real statement: 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 three: Use the syntax sugar @ to decorate 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, equivalent to "MyFunc = Deco (myfunc)" but found that the new function was invoked only for the first time, and the original function called the ' Def Deco (func) more than once: print (" Before MyFunc () called. ") Func () print ("After MyFunc () called.") return func @deco def myfunc (): Print ("MyFunc () called.") MyFunc () MyFunc ()

Step Fourth: Use the inline wrapper function to make sure 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 the inline wrapper function to ensure that each new function is called, the inline wrapper function's formal parameter and return value are the same as the original function, and the adorner returns the inline wrapper function object ' Def deco (func): Def _deco (): P Rint ("before MyFunc () called.") func () print ("After MyFunc () called.") # do not need to return func, you should actually return the return value of the original function returns _deco @deco def m Yfunc (): Print ("MyFunc () called.") return ' OK ' MyFunc () MyFunc ()

Step Fifth: Decorate the function 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 inline wrapper function has the same formal parameter and return value as the original function, and the adorner returns the inline wrapper function object ' Def deco (func): Def _deco (A, B): print ("b Efore MyFunc () called. ") ret = func (A, b) print ("After MyFunc () called." Result:%s '% ret ' return ret _deco @deco def myfunc (A, B): Print ("MyFunc (%s,%s) called."% (A, b)) return a + b m Yfunc (1, 2) MyFunc (3, 4)

Step Sixth: Decorate a function with an indeterminate 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 indeterminate number of parameters (*args, **kwargs), automatically adapt to variable parameters and named parameters ' Def deco (func): Def _deco (*args, **kwarg s): Print ("before%s called."% func.__name__) ret = func (*args, **kwargs) print ("after%s called.") Result:%s '% (func.__name__, ret)) return to 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)

Seventh step: Let the adorner take the parameter

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 #-*-CODING:GBK-*-' Example 7: On the basis of example 4, let the adorner take a parameter, compared to the previous example, there is a layer of wrapper over the outer layer. The decorated function name should actually be more meaningful. ' Def deco (ARG): Def _deco (func): Def __deco (): Print ("before%s called [%s]."% (func.__name__, arg)) fun C () print (' after%s called [%s]. '% (func.__name__, arg)) return __deco return _deco @deco ("MyModule") def myfunc (): Prin T ("MyFunc () called.") @deco ("Module2") def MYFUNC2 (): Print ("Myfunc2 () called.") MyFunc () Myfunc2 ()

Eighth step: Let the adorner with 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: Adorner with class parameters ' class Locker:def __init__ (self): print ("locker.__init__ () should" is not called. " @staticmethod def acquire (): Print ("Locker.acquire () called. (This is a static method)") @staticmethod def release (): Print (" Locker.release () called (no object instance) ") def deco (CLS): ' The CLS must implement 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 __de CO return _deco @deco (locker) def myfunc (): Print ("MyFunc () called.") MyFunc () MyFunc ()

Nineth Step: Adorners with class parameters, and split public classes into other py files, while demonstrating the application of multiple adorners 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__ ()" Call ed. ") @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): ' The CLS must implement 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.u Nlock () 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: adorners with class parameters, and split common classes into other PY files also demonstrates applying multiple adorners 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 your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.