[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Nested module import clutters package namespace?

Gabriel Genellina

2/29/2008 12:16:00 AM

En Thu, 28 Feb 2008 11:00:08 -0200, Dr. Rüdiger Kupper
<ruediger.kupper@honda-ri.de> escribió:

> I'd be grateful for help with a problem of package and module
> namespaces. The behaviour I observe is unexpected (to me), and I
> couldn't find the answer in the docs, the tutorial, or the mailing
> list archive. So here we go:
>
> I have a package named 'pack'. Apart from the '__init__.py' file the
> directory contains two modules 'x.py' and 'y.py':
>
> pack/
> __init__.py
> x.py
> y.py
>
> ==== __init__.py ====
> print "pack: Here is pack."
> print "pack: I now assign y='hello'."
> y="hello"
> print "pack: My y is now:", repr(y)
> print "pack: I now 'import x' which in turn does 'import y as q'."
> import x
> print "pack: My y is now:", repr(y)
> print "pack: Why?"
> =====================
>
> ==== x.py ===========
> print ' x: Here is x.'
> print " x: I now 'import y as q'."
> import y as q
> =====================
>
> ==== y.py ===========
> print ' y: Here is y.'
> =====================

Looks like a bug to me - or perhaps someone who actually understands how
import works could explain it?

http://bugs.p...

--
Gabriel Genellina

2 Answers

Arnaud Delobelle

2/29/2008 12:59:00 AM

0

On Feb 29, 12:16 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Thu, 28 Feb 2008 11:00:08 -0200, Dr. Rüdiger Kupper  
> <ruediger.kup...@honda-ri.de> escribió:
>
>
>
> > I'd be grateful for help with a problem of package and module
> > namespaces. The behaviour I observe is unexpected (to me), and I
> > couldn't find the answer in the docs, the tutorial, or the mailing
> > list archive. So here we go:
>
> > I have a package named 'pack'. Apart from the '__init__.py' file the
> > directory contains two modules 'x.py' and 'y.py':
>
> > pack/
> >       __init__.py
> >       x.py
> >       y.py
>
> > ==== __init__.py ====
> > print "pack: Here is pack."
> > print "pack: I now assign y='hello'."
> > y="hello"
> > print "pack: My y is now:", repr(y)
> > print "pack: I now 'import x' which in turn does 'import y as q'."
> > import x
> > print "pack: My y is now:", repr(y)
> > print "pack: Why?"
> > =====================
>
> > ==== x.py ===========
> > print '  x: Here is x.'
> > print "  x: I now 'import y as q'."
> > import y as q
> > =====================
>
> > ==== y.py ===========
> > print '    y: Here is y.'
> > =====================
>
> Looks like a bug to me - or perhaps someone who actually understands how  
> import works could explain it?

Say:

pack/
__init__.py
x.py

importing x in any manner will automatically load pack and put x in
pack's namespace. Try:

from pack import x as foo
sys.modules['pack'].x

This explain the behaviour observed by the OP. AFAIK it is not a
bug. The tutoria hints at this without going into too much detail.

--
Arnaud

Dr. Rüdiger Kupper

2/29/2008 12:19:00 PM

0

Arnaud Delobelle wrote:
> On Feb 29, 12:16 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>> En Thu, 28 Feb 2008 11:00:08 -0200, Dr. Rüdiger Kupper
>> <ruediger.kup...@honda-ri.de> escribió:
>>
>>
>>
>>> I'd be grateful for help with a problem of package and module
>>> namespaces. The behaviour I observe is unexpected (to me), and I
>>> couldn't find the answer in the docs, the tutorial, or the mailing
>>> list archive. So here we go:
>>> I have a package named 'pack'. Apart from the '__init__.py' file the
>>> directory contains two modules 'x.py' and 'y.py':
>>> pack/
>>> __init__.py
>>> x.py
>>> y.py
>>> ==== __init__.py ====
>>> print "pack: Here is pack."
>>> print "pack: I now assign y='hello'."
>>> y="hello"
>>> print "pack: My y is now:", repr(y)
>>> print "pack: I now 'import x' which in turn does 'import y as q'."
>>> import x
>>> print "pack: My y is now:", repr(y)
>>> print "pack: Why?"
>>> =====================
>>> ==== x.py ===========
>>> print ' x: Here is x.'
>>> print " x: I now 'import y as q'."
>>> import y as q
>>> =====================
>>> ==== y.py ===========
>>> print ' y: Here is y.'
>>> =====================
>> Looks like a bug to me - or perhaps someone who actually understands how
>> import works could explain it?
>
> Say:
>
> pack/
> __init__.py
> x.py
>
> importing x in any manner will automatically load pack and put x in
> pack's namespace. Try:
>
> from pack import x as foo
> sys.modules['pack'].x
>
> This explain the behaviour observed by the OP. AFAIK it is not a
> bug. The tutoria hints at this without going into too much detail.

Thanks Gabriel and Arnaud for your comments. Since I felt unable to
decide if this is a bug or "known behaviour" I filed the issue in
the bugtracker. It certainly is inconsistent with what the docs and
tutorial tells the user (they all say that from the user's view,
import statements behave like assignments), so it should at least be
clearly documented.

Cheers,
Rüdiger

--
Dr. Rüdiger Kupper
Honda Research Institute Europe GmbH
Carl-Legien-Straße 30
D-63073 Offenbach/Main, Germany

Phone : +049 (0)69-890 11-725
Fax : +049 (0)69-890 11-749
E-Mail: ruediger.kupper@honda-ri.de
PGP ID: C2303358