(question)
Question:
Answer:
Question:
#!/usr/bin/python
def outer():
i=1
def inner():
print "i=%s" % str(i)
i += 1
inner()
inner()
outer()
This program fails due to a scoping error:
This issue is further complicated by Python's lack of a way to access parent variables which are not global. In this case, there are three nested scopes:
This version of the code works:
Python has an unusual scoping system, where functions are pre-scanned for assignments before they are executed. Any variable which gets assigned is assumed to be local unless explicitly declaredTraceback (most recent call last): File "q/function_scope-2.py", line 12, in ? outer() File "q/function_scope-2.py", line 9, in outer inner() File "q/function_scope-2.py", line 6, in inner print "i=%s" % str(i) UnboundLocalError: local variable 'i' referenced before assignment
global
.
This issue is further complicated by Python's lack of a way to access parent variables which are not global. In this case, there are three nested scopes:
global
, outer
, and inner
. But
inner
has no direct way to access outer
's data for
assignment. The only way to assign data from inner
to outer
is to wrap the data in containers so Python's optimizer will not detect any
assignment, and will not create local variables to override the parent data.
This version of the code works:
#!/usr/bin/python def outer(): i=[1] def inner(): print "i=%s" % str(i[0]) i[0] += 1 inner() inner() outer()