[Tutor] Name spaces again

Kent Johnson kent37 at tds.net
Mon Apr 25 00:11:54 CEST 2005


David Driver wrote:
> I have read the essay at
> <http://www.python.org/doc/essays/packages.html> and I am still having
> problems understanding what I need to do. 

> In AR.sobjs I have a class that is inherited from
> Common.sobjs.baseDomObj. When I run StartHere.py Common imports just
> fine. It starts to import AR and AR. __init__.py imports AR.sobjs.py
> and it throws the following error:
> 
> Traceback (most recent call last):
>   File "C:\foo\doinit\starthere.py", line 3, in ?
>     import AR
>   File "C:\foo\doinit\AR\__init__.py", line 3, in ?
>     import sobjs
>   File "C:\foo\doinit\AR\sobjs.py", line 1, in ?
>     class custAddress(Common.address):
> NameError: name 'Common' is not defined
> 
> Do I need to import Common in each sub package to have access to it?

Yes, you have to import Common in any module that references Common.

> Does it consume more memory?

No, not a significant amount. The module itself is only loaded once; each client module keeps a 
reference to the loaded module, so it is just the cost of a name.

  What about Sub Sub packages? shouldn't
> you be able to Import DateTime in StartHere and have all sub
> modules/packages that are imported into StartHere.py use it?

No, that's not the way it works. There is no truly global namespace in Python, you have to import 
the names you need into the modules that need them. (OK, there is one global namespace but you 
generally shouldn't change it.)

  Am I
> making a big deal out of nothing?

Maybe - are you making a big deal out of it? :-)

Kent



More information about the Tutor mailing list