problem with namespaces using eval and exec

Jens jens.goepfert at tracetronic.de
Tue May 16 05:05:12 EDT 2006


Hi,

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