Namespace question...

Darrell news at dorb.com
Thu Nov 4 20:23:56 EST 1999


Untested code:

sys.modules['__main__'].Version

I'm not sure this is a good idea. If your code becomes part of some greater
application it won't work. A slight improvement might be something like
this.

sys.modules['__keeperOfTheVersion']=sys.modules[__name__]

--
--Darrell
Eric Jacobs <x at x.x> wrote in message news:3820D46C.517EA278 at x.x...
> oofoe at my-deja.com wrote:
> >
> > Hi!
> >
> > How can you access the global namespace in python 1.5.1? I'd like to get
> > a version string which is declared in the main script file. However,
> > each module also has a version string which overrides the original one.
> > I tried to access it as __main__.Version, similarly to $main::Version in
> > Perl, which is what I'm more familiar with. Of course, it didn't work.
> >
> > I have something like this:
> >
> > module file foo.py:
> > ------------------------------
> > Version = '$Id$'
> >
> > def bar():
> >    print "Version is: ", __main__.Version
> >
> > main program:
> > ------------------------------
> > Version = '$Id$'
> >
> > import foo
> >
> > foo.bar()
> >
> > -------------------------------
> >
> > I'd certainly appreciate any pointers. The namespace stuff seems to get
> > a little hazy around issues like this.
>
> Import it first! Use import __main__.
>
> I think some of this confusion arises because there is no global
> namespace in the sense of a area that exists above all modules
> (a good design design, IMO). The word global simply refers to
> variables at the level of the module, as opposed to within a
> function.
>
> --
> http://www.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list