Python's Path to Growth "fifth": the Python-based decorator

Source: Internet
Author: User

First, what is an adorner

Decoration: decoration is both cosmetic and means adding new functions to other functions

Device: Both functions

Adorner definition: The essence is the function, function is to add new functions for other functions

Second, the adorner needs to follow the principle

1, can not modify the source code of the adorner (open closed principle)

2. After adding a new function to the adorner function, you cannot modify the function's Calling method

Third, realize the knowledge reserve of the decorator

Adorner = higher order function + function nesting + closure

Definition of higher order functions of higher order functions:

1, the function receives the parameter is a functional name

2. The return value of a function is a functional name

3, meet the above conditions any one, can be called the higher-order function

1 deffoo ():2     Print('My function name is passed as a parameter to the higher order function')3 defGao_jie1 (func):4     Print('I am the higher order function 1, the parameter name I receive is%s'%func)5 func ()6 7 defGao_jie2 (func):8     Print('I am the higher order function 2, my return value is%s'%func)9     returnfuncTen  One gao_jie1 (foo) AGao_jie2 (foo)
High- order function Demonstration
1 #High- Order function Application 1: Passing function names as parameters to higher-order functions2 Import Time3 deffoo ():4     Print('From the foo')5 6 defTimmer (func):7Start_time=time.time ()8 func ()9Stop_time=time.time ()Ten     Print('function%s Run time is%s'% (func,stop_time-start_time)) One Timmer (foo) A #Summary: We did add the Foo runtime function to the function foo, but Foo was originally executed in Foo (), and now we need to call the higher-order function Timmer (foo), changing the way the function is called
pass function names as arguments to higher-order functions
1 #High -Order function Application 2: The function name as a parameter to the higher-order function, the higher-order function directly return functions of the name2 Import Time3 deffoo ():4     Print('From the foo')5 6 defTimmer (func):7Start_time=time.time ()8     returnfunc9Stop_time=time.time ()Ten     Print('function%s Run time is%s'% (func,stop_time-start_time)) Onefoo=Timmer (foo) A foo () - #Summary: We did not change the way Foo was invoked, but we did not add any new features to Foo
The function return value is the name of the functor

Summary of higher-order functions:

1, the function receives the parameter is a functional name

Function: Add a new function to a function without modifying the source code of the function

Insufficient: Changes the way the function is called

2. The return value of a function is a functional name

Function: Does not modify the function's Calling method

Insufficient: Cannot add new features

Five, function nesting
def foo (name):    print ("from foo%s"%name)    def ret ():        print (' from ret ')        def Bar ():            print ("From Bar" )        Bar ()    ret ()    foo ("Albert")

  

Python's Path to Growth "fifth": the Python-based decorator

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.