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 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