Python function--Adorner advanced

Source: Internet
Author: User

Open Closure principle

1. Open for expansion

Why open to extensions?

We say that no one program can have all the features in mind at the beginning of the design and will not make any updates or changes in the future. So we have to allow code extensions, add new features.

2. The modification is closed

Why should we be closed to the changes?

As we have just mentioned, as we write a function that is likely to have been delivered to someone else, if we modify it at this time, it is likely to affect other users who are already using the function.

The adorner perfectly follows this open closure principle.

*args,**kwargs

def f1 (*args,**kwargs): When the function is received, * is an aggregation

Print (*args) execution function value, * is scattered

Useful information for functions

Print (f1.__doc__) Gets the description information for the function

Print (f1.__name__) Gets the name of the function

Adorners are perfect, but sometimes the way we normally look at function information is invalidated here:

def index ():     " " This is a home page information ""    Print ('fromindex') Print (Index. __doc__)    # to view the method of a function comment # #这是一个主页信息 Print (Index. __name__)   # How to view the function name # #index

How to solve it? 1 from functools import wraps

2 3 defDeco (func):4@wraps (func)#plus above the inner function5     defWrapper (*args,**Kwargs):6         returnFunc (*args,**Kwargs)7     returnwrapper8 9 @decoTen defindex (): One     " "haha haha" " A     Print('From index') -  - Print(Index.__doc__) the Print(Index.__name__)


"
"

haha haha
Index

‘‘‘

Adorner Advanced:

Adorner with parameters:

Flag = True#Adorner with Parametersdefouter (f):defTimmer (func):defInner (*args,**Kwargs):ifF:Print(" "what to do before executing a function" ") ret= Func (*args,**Kwargs)ifF:Print(" "what to do after the function is executed" ")            returnretreturnInnerreturntimmer@outer (flag)#equivalent to @timmerdeffunc ():Print(111) func ()" "The 111 execution function to do before executing the function" "

Multiple adorners decorate a function:

defWrapper1 (func):#Fun = f    definner ():Print('Wrapper1, before Func') func ()Print('Wrapper1, after Func')    returnInnerdefWrapper2 (func):#func = Inner1    definner ():Print('Wrapper2, before Func') func ()Print('Wrapper2, after Func')    returnInner@wrapper2#f = wrapper2 (f) inside the f=inner1 outside of the F=inner2@wrapper1#f = wrapper1 (f) return inner1 F = inner1deff ():Print('In F') F ()#Inner2 ()" "Wrapper2, before Funcwrapper1, before Funcin Fwrapper1, after Funcwrapper2, after Func" "

Python function--Adorner advanced

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.