python: how binding works
I am trying to understand, how exactly variable binding in python works. Let's look at this:
def foo(x):
def bar():
print y
return bar
y = 5
bar = foo(2)
bar()
This prints 5 which seems reasonable to me.
def foo(x):
def bar():
print x
return bar
x = 5
bar = foo(2)
bar()
This prints 2, which is strange. In the first example python looks for the variable during execution, in the second when the method is created. Why is it so?
To be clear: this is very cool and works exactly as I would like it to. However, I am confused about how internal bar function gets its context. I would like to understand, what happens under the hood.
EDIT
I know, that local variables have greater priority. I am curious, how python knows during execution to take the argument from a function I have called previously. bar
was created in foo
and x
is not existing any more. It have bound this x
to the argument value when function was created?
The second example implements what is called a closure. The function bar
is referencing the variable x
from its surrounding context, ie the function foo
. This precedes the reference to the global variable x
.
See also this question Can you explain closures (as they relate to Python)?
The issue that you are alluding to is one of lexical vs dynamic scoping of variables in python. To be explicit, python defines the following four scopes.
In the first example, where "y" is defined outside of the function bar, python searched for the innermost scope and moved up the chain until it found the global variable "y" in the module
In the second example, where "x" is being defined by function foo(x), x belongs to the scope of an enclosing function, when its being printed inside bar. In pure terms, a closure has been defined.
For further study on scoping in python, I found the following article a great read
In both examples, the lookup happens at runtime. The only difference is that there's a locally defined variable x
, while there isn't a locally defined variable y
.
When executing ...
def foo(x):
def bar():
print y
return bar
y = 5
bar = foo(2)
bar()
... the print
statement looks up the variable named y
, and only finds it in the global context, so it uses that one, and prints "5".
In ...
def foo(x):
def bar():
print x
return bar
x = 5
bar = foo(2)
bar()
... when the lookup occurs, there is a scoped variable x
defined--which is fixed at "5" when the foo
function is called.
The key is that arguments are evaluated at the time they're passed into functions, so the outer function foo
evaluates the arguments passed in when it's called. This effectively creates a variable called x
in the context of the foo
function, so whenever bar
executes it sees that variable, and not the globally defined one.
This can occasionally be confusing, as in the following code:
lst = []
for i in range(5):
x = i
lst.append(lambda: x)
for func in lst:
print func() # prints 4 4 4 4 4
You need to do:
lst = []
for i in range(5):
def _func(x):
return lambda: x
lst.append(_func(i))
for func in lst:
print func() # prints 0 1 2 3 4
链接地址: http://www.djcxy.com/p/51114.html
下一篇: python:绑定是如何工作的