Understanding of Python closures and adorners

Source: Internet
Author: User
Tags closure

Closures:

Two nested functions, external functions return the internal function of the argument, external function?

def external function (parameter):

def intrinsic function ():

Pass

return intrinsic function

The difference between him and the function:

1. Format two nested functions

2. Parameters of the closure external function can be persisted in memory

What the adorner is: Closure plus @xxx

The work of the decorator: To add additional functionality without changing the original function value

The code for the adorner:

def set_fun (func):

def call_fun (*args,**kwargs):

return func (*args,**kwargs)

Return Call_fun

@set_fun

def test ():

Pass

1. Pre-decoration function test, which is directed by the Func 2. The function test after the decoration, the actual is Call_fun 3. Pre-decoration test, after the decoration test, after the decoration of the test (Call_fun) three to maintain? 4. The adorner cannot change the function's tune? Type and return value

The understanding of a function of a decorator?

A decorative decorator? A diagram of a function:

A decorator decorator? A function memory graph to understand:

Understanding of Python closures and adorners

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.