Python Decorator Supplement

Source: Internet
Author: User
Tags python decorator

Some functions are defined, and these functions are called externally, but before they are called, some of the same functions need to be implemented, in which case the adorner is the best solution:

def F1 ():     Print ("F1") def F2 ():     Print ("F2")

When called in another module

Import s1s1.f1 () s1.f2 ()
Execution Result:
F1
F2

At this point, we need to add the ability to print the log separately in f1,f2, so the definition of the function becomes

def F1 ():     Print ("log")     Print ("F1") def F2 ():     Print ("log")     Print ("F2")

The result of invoking execution in the module is:

Logf1logf2

This time the entire function to be modified, the workload is very large, and then think of a function to define the print function of the log

def log ():     Print ("log")
def F1 (): log () print ("F1")def F2 (): log () print ("F2")

By changing the code in this way, we can also implement the function, and only need to modify the contents of the function each time. However, this changes the contents of the F1,F2 function body, violating the open closure principle (do not modify within the function, can be modified outside the function)

defOuter (func):definner ():Print("Log")        func ()returnInner@outer#f1 = outer (F1)defF1 ():Print("F1") @outerdefF2 ():Print("F2")

The inner layer principle of the adorner: the @outer has two functions: 1, automatically executes the outer function, and passes it immediately below the function name F1 to the outer function, 2, assigns the outer function the return value to the F1 function again

Adorner with return value

defOuter (func):definner ():Print("before") func ()Print(" After")    returnInner#@outerdefF2 ():Print("F2")    return "SB"

In another module call, without the use of adorners (with return values):

r = s1.f2 ()print  (r) Results: F2SB

After the adorner has been added, the code changes to the following format (no record return value):

defOuter (func):definner ():Print("before") func ()Print(" After")    returnInner@outerdefF2 ():Print("F2")    return "SB"execution Result: Beforef2afternone

The printed result is inconsistent with what we need (we need to record the return value), so we re-modified the adorner code:

defOuter (func):definner ():Print("before") R=func ()Print(" After")        returnRreturnInner@outerdefF2 ():Print("F2")    return "SB"execution Result: BEFOREF2AFTERSB

To define an adorner with parameters

defOuter (func):defInner (a):Print("before") R=func (a)Print(" After")        returnRreturnInner@outerdefF2 (a):Print(a)return "SB"call R in the module= S1.f2 ("FFFFFF")Print(r) Execution Result: BEFOREFFFFFFAFTERSB

Python Decorator Supplement

Related Article

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.