Scope trouble with __init__.py

Mike Romberg romberg at smaug.fsl.noaa.gov
Fri Jul 5 20:02:28 EDT 2002


  This is a copy of what I entered into the sourceforge
bugtracker...

  I think that there is a small problem with what gets
imported into the scope of an __init__.py module with
a package.  At least if this is the correct behavior it
seems a bit funny to me.

  Consider the following modules located in a subdirectory
of the load path called spam:

spam/
 
--------  spam/__init__.py ---------------
a = 1
from b import c
print "a = ", a


-------- spam/b.py -------------------
c = 2
import a

--------- spam/a.py -----------------
d = 3

  I would expect that when one imported the
spam package, you would see 'a = 1' displayed.
Instead you get 'a =  <module 'spam.a' from 'spam/a.py'>'.
So, it seems that python pulled in the 'a' symbol from b
and overwrote the one already in __init__.  This would
make sense if __init__ did a 'from b import *'.  But
I think the current behavior might be a bug.

Mike Romberg (romberg at fsl.noaa.gov)



More information about the Python-list mailing list