This example describes the Python function adorner usage. Share to everyone for your reference. Specifically as follows:
Adorners are often used in the scene of the demand for cutting plane, the more classic is the insertion log, performance testing, transaction processing and so on. Adorners are an excellent design for solving this type of problem,
With the adorner, we can extract the same code that is irrelevant to the function itself in a large number of functions and continue to reuse it. In summary, the role of adorners is to add additional functionality to existing objects.
?
| 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 |
#! Coding=utf-8 Import Time Def Timeit (func): Def wrapper (a): start = Time.clock () func (1,2) end =time.clock () print ' used: ', End-start Print a return wrapper @timeit # foo = Timeit (foo) is completely equivalent, # after use, the Foo function is changed, equivalent to wrapper def foo (a,b): Pass #不带参数 Adorner # Wraper will be decorated with FN, return Wraper, the returned wraper is the decorated fn def test (func): Def wraper (): print "Test Start" func () print "En D start "return Wraper @test def foo (): print ' in Foo ' foo () |
Output:
?
| 1 2 3 |
Test start in Foo end start |
Adorners decorate a function with parameters:
?
| 1 2 3 4 5 6 7 8 9 10 |
def parameter_test (func): Def Wraper (a): print "Test Start" func (a) print "End start" return Wraper @parameter_test def PA Rameter_foo (a): print "Parameter_foo:" +a #parameter_foo (' Hello ') |
Output:
?
| 1 2 3 4 |
>>> Test start Parameter_foo:hello end start |
Adorner modifies the function of the number of indeterminate arguments:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
def much_test (func): Def wraper (*args, **kwargs): print "Test Start" func (*args, **kwargs) print "End Start" return Wraper @much_test def much1 (a): Print a @much_test def much2 (a,b,c,d): Print a,b,c,d much1 (' a ') Much2 (1,2,3,4) |
Output:
?
| 1 2 3 4 5 6 |
Test start a end start Test start 1 2 3 4 end start |
With the parameters of the adorner, and then a layer on the package can be:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
def TP (name,age): Def much_test (func): print ' in Much_test ' Def wraper (*args, **kwargs): print "Test start" Print str (name ), ' at: ' +str (age) Func (*args, **kwargs) print "End-start" return Wraper return much_test @tp (' One ', ') def tptest (Paramet ER): Print parameter tptest (' Python ... ') |
Output:
?
| 1 2 3 4 5 |
In much_test test start one at:10 python ... end start |
?
| 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 |
Class Locker:def __init__ (self): print ("locker.__init__ () should is not called.") @staticmethod def acquire (): Print ("loc Ker.acquire () called. (This is a static method) ") @staticmethod def release (): Print (" Locker.release () called. (no object instance required) def deco (CLS): ' " CLS must implement acquire and release static methods ' Def _deco (func): Def __deco (): Print ("before%s called [%s]."% (func.__name__, CLS)) CLS.A Cquire () Try:return func () finally:cls.release () return __deco return _deco @deco (locker) def myfunc (): Print ("MyFunc () Called. ") MyFunc () |
Output:
?
| 1 2 3 4 5 6 |
>>> before MyFunc called [__main__.locker]. Locker.acquire () called. (This is a static method) MyFunc () called. Locker.release () called. (no object instance required >>> |
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Output:
?
| 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 |
Before MyFunc called. Mylocker.acquire () called. MyFunc () called. Mylocker.unlock () called. Before MyFunc called. Mylocker.acquire () called. MyFunc () called. Mylocker.unlock () called. None before __deco called. Mylocker.acquire () called. Before Myfunc2 called. Lockerex.acquire () called. MYFUNC2 () called. Lockerex.unlock () called. Mylocker.unlock () called. 3 before __deco called. Mylocker.acquire () called. Before Myfunc2 called. Lockerex.acquire () called. MYFUNC2 () called. Lockerex.unlock () called. Mylocker.unlock () called. 7 |
I hope this article will help you with your Python programming.