Confused about nested scoping
Stephen R. Figgins
fig at monitor.net
Wed Apr 18 19:18:03 EDT 2001
I seem to be misunderstanding what nested scopes do.
>From AMK's summary of 2.1 changes:
Put simply, when a given variable name is not assigned a value
within a function (by an assignment, or the def, class, or import
statements), references to the variable will be looked up in the
local namespace of the enclosing scope.
Here is what I tried:
from __future__ import nested_scopes
def printspam():
print "spam has value %s" % (spam)
def scopetest():
spam = 'bacon'
printspam()
spam = 'eggs'
printspam()
scopetest()
I figured without nested scopes I would get
spam has value eggs
spam has value eggs
But I thought with nested scopes I would get
spam has value eggs
spam has value bacon
Because the second printspam's enclosing scope would be that of
scopetest in which I had reassigned spam.
But I still get eggs with my spam.
What am I misunderstanding?
-Stephen
More information about the Python-list
mailing list