Nested Scopes
Variable Access and Assignment
- Inner function can access variables defined in enclosing scopes.
- However, can not modify their values
- Assignment always binds to a name in the local scope or global scope
x = 2
def foo():
a = 1
x = 3
def bar():
global x
a = 10 # Creates local a
x = 11 # Modifies global x
bar()
print "a =",a
print "x =",x
foo() # prints "a = 1", "x = 3"
print x # prints "11"
|