eval and exec in an own namespace

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