namespace question

Alex Martelli aleaxit at yahoo.com
Fri Sep 3 02:11:35 EDT 2004


Elaine Jackson <elainejackson7355 at home.com> wrote:

> I would like to be able to write a function f, which will live in a module M,
> and will call a function g, such that, when f is imported from M into the
> interpreter, and invoked there, its invokation of g will return the
> interpreter's global namespace. Is there a way to do this and, if so, how?
> Muchas gracias for any and all assistance.

There is no "interpreter global namespace".  If you mean the namespace
of the calling module (which could be the interactive session's __main__
if that's who did the importing) you can get there via sys._getframe --
it's meant for debugging purposes (note the leading underscore) but it
will work.  If you mean the namespace of __main__ (which could be the
interactive session if there is one) you can get the __main__ module
anywhere as sys.modules['__main__'] and a vars call around that will
give the module's dictionary (which is, I assume, what you mean by
namespace).


Alex



More information about the Python-list mailing list