eval and exec in an own namespace

J

Jens

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top