package/namespace confusion

Bruce Edge bedge at troikanetworks.com
Tue Mar 6 18:05:14 EST 2001


It was the nested global thing that got me. I assumed global was global,
like C/tcl/everything else.
Maybe it should be named "parent" rather than global.

Thanks for the info, Bruce.

Ben Hutchings wrote:
> 
> Bruce Edge <bedge at troikanetworks.com> writes:
> 
> > Hi,
> > I'm sure this is a real rookie issue here, but I can't seem to define a
> > global then access it in a package.
> > I'm using __init__.py to define glob_var, then load util.py.
> >
> > I would have expected util/ufunc() to have access to glob_var.
> >
> > test/__init__.py:
> >
> > glob_var=0        <- is this where I should stick package globals?
> 
> Yes.
> 
> > __all__=["util"]
> >
> >
> > test/util.py:
> >
> > def ufunc():
> >       global glob_var
> <snip>
> 
> The global namespace for this function is the namespace of the module
> (test.util) not that of the package (test).
> 
> If you want package globals to be module globals too, then use
> 'from test import *' at the top of your modules.  Alternatively you
> can 'import test' and refer to 'test.glob_var'.
> 
> --
> Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list