Nested Scopes

Incompatibilities

  • exec and execfile()
     # This Python code is illegal
     def foo():
         x = 1
         def bar(a):
             return x + a
         exec "x = x + 1"
     
     SyntaxError: unqualified exec is not allowed in function 'foo' it 
     contains a nested function with free variables
  • Use of dynamic features problematic in inner functions
     # This code doesn't work
     def foo(y):
        x = 1
        def bar(a):
             return eval("x+a")       # name 'x' not defined
        return bar(y)
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 23
July 26, 2001, beazley@cs.uchicago.edu
>>>