problem with namespaces using eval and exec

Jens jens.goepfert at
Tue May 16 11:05:12 CEST 2006


has anyone an idea why the following code does not work.

s = """
def a(n):
  return n*n

def b(t):
  return a(t)

ns = {}
exec(s, {}, ns)
eval("b(2)", ns, {})

executing this script raises an exception (NameError: global name 'a'
is not defined) in the last line. 

Hope for your help.

More information about the Python-list mailing list