why is this namespace weirdness happening?

Preston Landers prestonlanders at my-deja.com
Wed Jan 10 11:43:21 EST 2001


 Hi All.

I noticed some namespace/import behavior that seems weird to me.  It
happens in both Python 1.5.2 and 2.0 (though the error message is
different.) Sorry if this is a FAQ; maybe someone could briefly explain
why this happens.

file1.py:---------------------

import file2

def foo():
    import file2
    # the line below fails if the line above is cmted out
    # but works fine if present
    print "file2 bar: ", file2.bar
    import file2
    print "file2 baz: ", file2.baz

foo()

file2.py:---------------------

bar = 1
baz = 2

------------------------------

It seems that on the file1 "bar" line, if the 'import file2' above it is
commented out, the file2.bar reference will fail (file2 being the
unrecognized item.)  Despite the 'import file2' at the top of the file.
It seems to be due to the 'import file2' following the statement.
Python seems to be scanning ahead for imports...?

I know that doing an import inside a function is not neccesary if you
have imported the module at the top of the file.  However, it seems
strange that it only fails if there is an odd number of import
statements for that module.

thanks!
Preston
pibble at yahoo dot com


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list