Nested Scopes

Implementation

  • Static scoping
  • Unbound names in inner functions only bind to statically defined names in outer function
  • This only occurs when code is compiled into Python byte-code.
     def foo():
         def bar():
             print x  # x bound to outer function
             print y  # y not known, use normal scoping rules (global)
         x = 4

Does not allow names to be dynamically generated

 def foo():
     def bar():
         print x
     exec "x = 4"  # Whoa!  
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 22
July 26, 2001, beazley@cs.uchicago.edu
>>>