Python Freedom Road (5) pyhton Closure

Source: Internet
Author: User

If a variable in the external scope (but not in the global scope) is referenced in an internal function, the internal function is considered as a closure ). closure is an important concept in functional programming. Syntax is relatively simple, but it is widely used.
In versions earlier than Python 2.1, only full local and local scopes are available. In Versions later than Python 2.1, we can use static nested fields. For example, in nested functions like the following, in the past, internal functions cannot access variables in the scope of external functions.
Def Foo ():
M = 3
Def bar ():
N = 4
Print m + n
Print m
Bar ()
In the current version, it can run perfectly, while m in bar () is a closure variable that is neither fully local nor local, it exists in the namespace and scope of a function-nested scope. The access rules in the nested scopes in the closure are the same as the global rules discussed above. That is, referencing m before the re-Declaration of the closure variable M will cause an exception.
Def Foo ():
M = 3
Def bar ():
Print M # unboundlocalerror
M = 4
Print m
Bar ()
Unboundlocalerror: local variable 'M' referenced before assignment
Why? In fact, it is related to the M type. We know that the basic data types in pyhton are variable and immutable. The assignment of immutable types is actually to redefine a new variable object, and copy the original object to the new object. For details, refer to the STR type description. If the above M is declared as a variable type list, this exception will not occur.
Def Foo ():
M = [3]
Def bar ():
Print M [0]
M [0] = 4
Print M [0]
Bar ()
For more information about variable types and immutable types, see API document.

The following is an example of a closure:
Def hellocounter (name ):
Count = [0]
Def counter ():
Count [0] + = 1
Print 'hello, ', name,', ', STR (count [0]) + 'Access! '
Return counter

Hello = hellocounter ('ysisl ')
Hello ()
Hello ()
Hello ()

Console output:
Hello, ysisl, 1 access!
Hello, ysisl, 2 access!
Hello, ysisl, 3 access!
In this example, hellocounter (name) returns a reference to the internal function counter, just like the pointer to the function in C ++, we save this reference with a variable hello, so this variable represents this counter function. Why does the repeated call to hello () maintain the auto-increment of the closure variable count, instead of re-allocating it after release? Because count is not a local variable of counter, it is a variable out of counter in the hellocounter () range, only when hellocounter (name) is called, count to be reassigned to the new memory address.

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.