Python Decorator closure function use method

Source: Internet
Author: User
Tags closure python decorator

To say that the decorator has to say the closure function in Python. Refer to the wiki introduction "in Computer science, closures (Closure) are abbreviations for lexical closures (lexical Closure) and are functions that refer to free variables. The referenced free variable will exist with this function, even if it has left the environment that created it. So, there is another saying that closures are entities that are combined by functions and reference environments associated with them. ”

Look, I feel a bit around the mouth, then, as I understand it: a function B in an internal function A that references a variable in the outer scope of B (but not in the global scope), then the internal function B is considered a closure (closure). For example, follow this explanation:


function A has function B, which refers to the x variable in B, where x refers to the deployment global variable x=10, and instead refers to the x=1 in a, then B is a closure function. Run Result:

x=1,y=2

Also, you can see that function B refers to the variable x in a.
Here is just a simple introduction, interested can go to study, next time to say closure of the application---modifiers.

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.