nested scopes clarification

Erik Price eprice at ptc.com
Mon Feb 24 23:49:13 CET 2003


Hi,

I'm reading "Python Essential Reference 2nd ed" by David Beazley, and 
have encountered the topic of Python's nested scopes in functions, and 
the lack thereof in Python 2.0 or earlier.  I was hoping someone could 
clarify this for me.

The book gives this example:

def bar():
   x = 10
   def spam():
     print 'x is ', x
   while x > 0:
     spam()
     x -= 1

Beazley writes:  "In this case, when the nested function spam() 
executes, its global namespace is the same as the global namespace for 
bar(), the module in which functions is defined.  As a result, spam() is 
unable to resolve any symbols in the namespace of bar() and fails with 
NameError."

Is that another way of saying that Python puts all of its function 
declarations into the same scope in pre-2.1 Pythons?



Thanks,

Erik






More information about the Python-list mailing list