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 =
The second example implements what is called a closure. The function bar
is referencing the variable x
from its surrounding context, i.e. 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
Nothing strange, it is because "x" from function argument has higher priority than global variable "x".
At first, global variables is a great evil.
Python has operator "global":
>>> def foo(x):
... def bar():
... global x
... print x
... return bar
...
>>> x = 5
>>> bar = foo(2)
>>> bar()
5
It is mater of scope, second example uses local scope variable x, that is preceded above globally declared
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