import X between submodules in a package

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Dec 19 12:50:47 EST 2007


En Wed, 19 Dec 2007 07:02:00 -0300, Chris <cwitts at gmail.com> escribió:

> #fp.py
> import cli
>
> #cli.py
> import os
>
> #config.py
> import cli
> print cli.os.environ['HOME']
>
> if you wish to use the os module loaded by the cli module

En Wed, 19 Dec 2007 09:42:31 -0300, Donn Ingle <donn.ingle at gmail.com>  
escribió:

> Chris wrote:
>> print cli.os.environ['HOME']
> I was really confused by your reply until I saw the cli.os part. Okay, I  
> see
> what you mean.

Note that altough this is perfectly legal, I would *not* recommend doing  
it unless you have a compelling reason to do so (like providing a single  
public namespace for a package, for example).
Some people choose to remove spurious names, to keep the namespace clean  
if that's important for other usage:

# config.py
import os, sys
startup_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
del os, sys
background_color = "white"
...

# main.py
import config
for key, value in vars(config).iteritems():
   print '%s=%r' % (key, value)

-- 
Gabriel Genellina




More information about the Python-list mailing list