problem with namespaces using eval and exec

Maric Michaud maric at aristote.info
Tue May 16 05:52:01 EDT 2006


Le Mardi 16 Mai 2006 11:05, Jens a écrit :
> s = """
> def a(n):
>   return n*n
>
>
> def b(t):
>   return a(t)
> """
>
>
> ns = {}
> exec(s, {}, ns)
> eval("b(2)", ns, {})
you passed an empty globals dictionnary to the eval func (the local one is not 
in the scope of b defintion)
try this :

exec s // like exec s in globals(), locals()
eval("b(2)") // like , eval("b(2)",  globals(), locals())

or if you to keep this dictionnary as a specific scope  in your appplication :

exec s in ns
eval("b(2)", ns)

-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list