Python's path to learning-the adorner of Python basics

Source: Internet
Author: User

Adorners are essentially a function, more intuitively, the adorner is equal to the higher order function + function nesting + closure, the adorner is a function with some basic function, this function can be added to other functions, so that the function of other functions more powerful. In addition, the adorner has two important characteristics:1, does not change the function of the call Method 2, does not change the modified function of the source code , that is, the adorner is in these two features to transform the function of the other functions.

A higher order function is a function in which the function receives a function or the return value of a function is a higher order function.

Function nesting is also a function inside the function, you can nest multiple layers.

A function closure refers to a function inside a function that uses a local variable of an external function.

#Outer wrapper for receiving the modified functiondefWrapper (fun):#an inner layer function that adds basic functionality to the modified function    defInner (*args,**Kwargs):#Basic Function Code        #call the decorated function and receive the return valueresult = Fun (*args,**Kwargs)#returns the received result        returnresult#equivalent to externally exposed inner layer functions    returnInner@wrapper#equivalent to: foo = wrapper (foo)defFoo (a,b,c=3):Print('Foo', A,b,c)
@wrapperdefBar ():Print('Bar')#Called directly, without changing the way the modified function is calledFoo () bar ()

If the wrapper also needs to pass parameters, you can continue to set a layer of functions outside the wrapper

Python's path to learning-the adorner of Python basics

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.