Learn python-function (adorner)

Source: Internet
Author: User

Adorner Open closure principle

Open: The extension to the function is open

Closed: The modification of the function is closed

The role of adorners

Add new functionality to the original function without changing the way the original function is called

#① Primer--Why should I have an adornerTo add new functionality to a function without modifying the original function, an adorner is created#② Simple Decoratordefdeco (f):defwrapper ():"""functions added before the original function"""f ()"""functions added after the original function"""    returnwrapperdeffunc ():Print('This is the original function! ') Func=Deco (Func) func ()#③ The grammatical sugar of the decoratordefdeco (f):defwrapper ():"""functions added before the original function"""f ()"""functions added after the original function"""    returnWrapper@deco#-the effect is equivalent to Func = Deco (func)deffunc ():Print('This is the original function') func ()#④ Adorner with return valuedefdeco (f):defwrapper ():"""functions added before the original function"""Res=f ()"""functions added after the original function"""        returnResreturnWrapper@decodeffunc ():Print('This is the original function') func ()#⑤ with parameters, adorner with return valuedefdeco (f):defWrapper (*args,**Kwargs):"""functions added before the original function"""Res= f (*args,**Kwargs)"""functions added after the original function"""        returnResreturnWrapper@decodefFunc (*args,**Kwargs):Print('This is the original function') func (*args,**Kwargs)#⑥ Multilayer Decorative Device#Todo#⑦ multiple adorners to modify the same function#Todo
Fixed format for adorners
defdeco (f):defWrapper (*args,**Kwargs):"""functions added before the original function"""Res= f (*args,**Kwargs)"""functions added after the original function"""    returnResreturnWrapper@decodefFunc (*args,**Kwargs): Pring ('This is the original function') func (*args,**kwargs)
Fixed format for adorners-wraps version

If you want to use the dual-down method of the original function, then you need to call the system adorner @ Wraps (func)

 fromFunctoolsImportWrapsdefDeco (func): @wraps (func)#plus above the inner function    defWrapper (*args,**Kwargs):returnFunc (*args,**Kwargs)returnWrapper@decodefOrigin_func ():" "here is the comment for the original function: return:" "    Print('This is the original function')#Although the adorner has been executed, Origin_func has already pointed to wrapper, but if the @wraps (func) adorner is used, the double-down method called Origin_func is still the original function Origin_funcPrint(Origin_func.__name__)>>>Origin_funcPrint(Origin_func.__doc__)>>>This is the comment of the original function>>>:return:
Adorner with parameters
defouter (flag):defTimer (func):defInner (*args,**Kwargs):ifflag:Print(" "what to do before executing a function" ") Re= Func (*args,**Kwargs)ifflag:Print(" "what to do after the function is executed" ")            returnRereturnInnerreturnTimer#The function call is executed here outer (False), return timer-> @timer->func = Timer (func), Func = inner@outer (False)deffunc ():Print(111) func ()
Multiple adorners decorate the same function
defWrapper1 (func):definner1 ():Print('Wrapper1, before Func') func ()Print('Wrapper1, after Func')    returnInner1defWrapper2 (func):definner2 ():Print('Wrapper2, before Func') func ()Print('Wrapper2, after Func')    returnInner2@wrapper2#decorate the inner1, i.e. Inner1 = Wrapper2 (inner1) = Inner2@wrapper1#execute this adorner first, i.e. F = wrapper1 (f) = Inner1deff ():Print('In F') F ()#Results>>>Wrapper2, before Func>>>Wrapper1, before Func>>>inchF>>>Wrapper1, after Func>>> Wrapper2, after Func

Learn python-function (adorner)

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.