The title looks very tiger, but it cannot be called analysis. I still have a lack of skills in this aspect. I may have been okay in the past. now I have less time and energy to study languages. If you have an incorrect explanation, you are welcome. The title looks very sloppy. In fact, it cannot be called an analysis. I still have a lack of skills in this aspect. I may have been okay in the past. now I have less time and energy to study languages. If there is something wrong with the explanation, you are welcome to come to the panel. don't mislead the public.
Let's talk about this issue directly. today @ neiddy (javaeye) talked to me about the closure issue. it's interesting to see the several examples and the impulse to understand them.
Take a look at the two sections of code:
>>> def foo(): a = 1 def bar(): a = a +1 return a return bar() >>> foo()Traceback (most recent call last): File "
", line 1, in
foo() File "
", line 6, in foo return bar() File "
", line 4, in bar a = a +1UnboundLocalError: local variable 'a' referenced before assignment
>>> def foo(): a = [1] def bar(): a[0] = a[0] + 1 return a[0] return bar() >>> foo()2
Using closures to experience functional programming is good. Parentheses are missing in the original text, otherwise the returned function itself will be boring. Let's talk about this magical phenomenon. it's okay to change it to a container. why?
Google does not find a way out for memory management and memory allocation, so it can only go to the source code. I have never seen that the efficiency is too low. Fortunately, some people have summarized it. I recommend "deep Python Programming", which is a low-key concept.
Code Analysis of the direct closure section has been mentioned in the section of the closure, but there is no mechanism dedicated to this strange problem.
Let's repeat the reading of the book, referring to the 'parameter' and 'closures in the book. let's talk about my understanding based on his explanation and the source code attached.
As mentioned at the end of the chapter, "the principle of implementing closure in CPython is not complicated. to put it bluntly, it is to append the referenced outer object to the inner function object (func_closure) that is re-created each time )."
This sentence is a summary of the previous section and also contains important information, it means that the objects corresponding to the internal function access the variables in the outer function is actually accessed by referencing the outer variables to the stack of the memory object, which is passed by value in the C language code.
For example, in the first code, a is actually referenced by 1, and its co_nlocals is 1, that is, a local variable is a before the equal sign (this is not true, I just want to help me understand this problem ). Since it is a local variable a, a = a + 1 must throw an UnboundLocalError.
For the second problem, although there is the same situation, even if the value is passed, the pointer of each position in the array will not change, and the value of the specified position will be modified, therefore, a container object is feasible.
I hate myself for this. I always feel that I am not clear about it. In fact, it is a pointer-type game that I often play when I first learned C language. although it is very bad, I hope it is critical. The next step is very simple. follow this idea to verify a piece of code, that is, if you only output this value and do not set local variables, it should be able to run.
>>> def foo(): a = 1 def bar(): return a return bar() >>> foo()1
It happened exactly as expected. Let's take a closer look at the code with the rain marks. Understanding the mechanism goes further. Python is getting more and more interesting. it is a lot of benefit to write code using python ideas.
By the way
>>> flist = []>>> for i in range(3): def foo(x): print x + i flist.append(foo) >>> for f in flist: f(2) 444
This code is mentioned in this article because I is not destroyed. this is also available when I search for memory allocation today. When programming, we should try to use list comprehension to reduce for and while. first, functional programming is concise and clear, and performance is improved and a counter is saved.