How to use the exec statement

Alex Martelli aleax at aleax.it
Sat Oct 5 10:41:52 EDT 2002


JB wrote:

> I should like to unse the exec statement. The code should be
> executed in the global namespace of the current module, but
> I do not know the name of the name space deictionary.

Built-in function globals() returns the global namespace's
dictionary.  But it's extremely unlikely that exec is the best
solution to your problem, particularly exec in an existing
namespace -- you have no control of what names defined
in that namespace get trampled.  If you explain what you're
trying to do with exec, it's possible that better suggestions
can be advanced regarding how to perform that.


Alex




More information about the Python-list mailing list