Some of the things you need to be aware of when using closures in Python programming

Source: Internet
Author: User
Tags definition closure

This article mainly introduces Python programming in the use of closures need to pay attention to some of the places, the article from the well-known Python developers Felinx blog, need friends can refer to the

Write this blog, originating from the Tornado Mail Group of this question how to use outer variable in inner method, which the foreigner's answer is very reference value, the key points are basically said. I'm here with some interesting examples to do some parsing, a brief description of the Python closure rules, first of all look at a classic example:

?

1 2 3 4 5 6 7 8 9 10 11 def foo (): a = 1 def bar (): a = a + 1 # print a + 1 # b = A + 1 # a = 1 print ID (a) bar () print A, id (a)

Running this function on python2.x will report unboundlocalerror:local variable ' a ' referenced before assignment that the local variable is undefined before the reference, how to understand the error? PEP 227 introduces that the Python parser searches for the definition of a variable based on the following level three rule:

The Python 2.0 definition specifies exactly three namespaces to check for each name-the local namespace, the global name Space, and the Builtin namespace.

Here the local may actually have multi-level, the above code is an example, the following by making some simple changes to the code to step by step to understand the rules here:

If you change the sentence a = a + 1 to print a + 1 or b = A + 1, there is no problem, that is, in the internal function bar, a in the outer function foo is actually visible and can be referenced.

It is no problem to change a = a + 1 to a = 1. But if you print out the IDs of two occurrences of a, you will find that these two Aces are not the same thing, and in the internal function bar, the local A = 1 defines a new local variable within the range of bar functions, Because the name is the same as the variable A in the outer function foo, the A In the outer function foo is practically invisible in the internal function bar.

Again, a = a + 1 What's wrong with the first A = XXX in this form, the Python parser thinks to create a new local variable A in the internal function bar, while a in the external function foo is not visible in bar, and the parser is on the right side of a + The 1 resolution is to use the local variable a plus 1, and then the left of a that is the local variable A has not been created (and so on the right side of the assignment), so this produces a chicken egg or egg chicken problem, resulting in the above said Unboundlocalerror error.

To solve this problem, there are two main programmes in python2.x:

Instead of the alias B = A + 1, the internal function bar refers only to a in the outer function foo.

Set a container for a in Foo, such as List

?

1 2 3 4 5 6 7 def foo (): a = [1,] def bar (): a[0] = a[0] + 1 bar () print a[0]

Of course this is sometimes very inconvenient, so in python3.x introduced a nonloacal keyword to solve this problem, as long as a = a + 1 before adding a nonloacal a can, that is, explicitly specify a is not internal function bar local variables, This allows for the normal use and redistribution of variable A in the external function foo within bar.

In the search for Python closures related materials, I found an interesting question about the Python closure in StackOverflow, interested in thinking about what to do and what the result should be? What is your expected outcome, and if not, how should you change the results you expect?

?

1 2 3 4 5 6 7 8 Flist = [] for i-xrange (3): def func (x): return x * I flist.append (func) for F in Flist:print F (2)

Note < > : More Wonderful tutorials please focus on Triple programming

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.