eval and exec in an own namespace

Jens jens.goepfert at tracetronic.de
Tue May 9 13:54:30 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