Python Decorator This thing for beginners is a pit, it is easy to go around the halo, the author at that time, I took a few days to explore the adorner, looked at different lecturers on the content of the explanation, or smattering.
But the decorator in the development is very useful, it is necessary to break, I hope this article can help learners quickly overcome the difficulties.
Preliminary understanding
# first look at a simple function def show (): print (' Mr tu ') show () # execution result  :MR tu# Now we extend this function with adorners, print a line " hello " before printing " Mr tu " def decorate (FUN1): def wapper (): print (' Hello ') fun1 () return wapper@decoratedef show (): print (' Mr tu ') show () # execution result : hellomr tu# now explains the above code. # 1, First def decorate (FUN1) defines an adorner function, in which a Wapper sub-function is defined, and the child function can inherit the parameters of the parent function. # 2, @ ' function name ' is a python syntax sugar @decorate equals decorate (show) # or dizzy, don't you understand? Okay, look at the code below: Def decorate (FUN1): def wapper (): print (' Hello ') fun1 () return&nbsP;wapperdef show (): print (' Mr tu ') f1 = decorate (show) F1 () # Execution results :helloMr tu# in this way, the effect is the same, because that is the nature of @decorate . # is the function show decorated by the adorner as an argument to the adorner function. # Summary execution Process: # 1, show function as parameters to the adorner function decorate , then fun1 = show# 2, This is done to the adorner's child function wapper, the child function can inherit the parameters of the parent function, so you can call fun1 # 3, and then Wapper function execution print print a line " Hello , then call FUN1 () -- here is actually the show function executed. because the show function is assigned as a parameter at the beginning of the adorner's execution. fun1.# now understand, as long as here understand, the following is very good understanding.
Functions for decorating with parameters
# a parameter Def decorate (FUN1): def wapper (arg1): print (' Hello ') fun1 (arg1) print (' nice to meet you ') return wapper@decoratedef show (arg1): print (arg1) show (' Mr alice ') # execution results:hellomr alicenice to meet you# two parameters Def decorate ( FUN1): def wapper (ARG1,ARG2): print (' Hello ') fun1 (ARG1,ARG2) print (' nice to meet you ') return wapper@decoratedef show (ARG1,ARG2): print (ARG1,ARG2) Show (' Mr alice ', ' Mr Tom ') # Execution Result: Hello (' Mr alice ',   ' Mr tom ') nice to meet you# n parameters Def decorate (fun1): def wapper (*args,**kwargs): print (' hello ')      FUN1 (*args,**kwargs) print (' Nice to meet you ') return wapper@decoratedef show (*args,**kwargs): print (Args[0]) print (args[1]) print (Args[2]) show (' Mr alice ', ' mr tim ', ' mr tu ') # execution Result: HELLOMR ALICEMR timmr tunice to meet you
A function is decorated with multiple adorners
DEF DECORATE01 (FUN1): def wapper (*args,**kwargs): print (' Hello world ') print (' I am decorate01 ') fun1 (*args,**kwargs)   RETURN WAPPERDEF DECORATE02 (FUN1): def wapper (*args,**kwargs ): print (' I am decorate02 ')      FUN1 (*args,**kwargs) print (' Nice to meet you ') return wapper@decorate01@decorate02def show (* Args,**kwargs): print (Args[0]) print (args[1]) print (Args[2]) show (' Mr alice ', ' mr tim ', ' mr tu ') # execution Results:hello Worldi am decorate01i am decorate02mr alicemr timmr tunice to meet you# Observe that the print placement is different, and the corresponding output results are different. DEF DECORATE01 (FUN1): def wapper (*args,**kwargs): print (' Hello world ') fun1 (*args,** Kwargs) print (' i am decorate01 ') # Replaced with the following     RETURN WAPPERDEF DECORATE02 (FUN1): def Wapper (*args,**kwargs): print (' I am decorate02 ' ) fun1 (*args,**kwargs) print (' nice to meet you ') return Wapper@decorate01@decorate02def show (*args,**kwargs): print (Args[0]) print (args[1]) print (args[2]) show (' Mr alice ', ' mr tim ', ' mr tu ') # execution results: Hello worldi am decorate02mr alicemr timmr tunice to meet youi am decorate01
Adorner function extension
#!/usr/local/python27/bin/python2.7def before (Request,*args,**kwargs): print (' Before ') Def after (Request,*args,**kwargs): print (' after ') Def filter (before_ Func,after_func): def outer (FUN1): Def wapper (Request,*args,**kwargs): before_result = before_func (Request,*args,**kwargs) if (Before_result != none): return before_result;        FUN1_RESULT = FUN1 (Request,*args,**kwargs) if (Fun1_result != none): return fun1_result; after_result = after_func (Request,*args,**kwargs) if (After_result != none): return after_ result; return wapper return Outer@filter (Before,after) def show (Request,*args,**kwargs): print (' Mr Tu ') show (' 1 ') # execution Result: beforemr tuafter
If the function does not define a return value, the Execute successful return value defaults to None, where the statement if (Before_result! = None) means what to do if the before function fails.
This article is from the "Break Comfort zone" blog, so be sure to keep this source http://tchuairen.blog.51cto.com/3848118/1843966
How Python understands dizzy and useful adorners