package/namespace confusion

Ben Hutchings ben.hutchings at roundpoint.com
Tue Mar 6 17:34:27 EST 2001


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