Namespace question...

Eric Jacobs x at x.x
Wed Nov 3 19:33:48 EST 1999


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.




More information about the Python-list mailing list