"import X" and "from X import y" produce different results, Why?

Parzival Herzog parz at home.com
Thu Mar 1 01:46:38 EST 2001


In the Essential Python Reference, it states (p. 72):

"The from statement is identical to import, e3xcept that instead of creating
a
name referring to the newly created module namespace, references to one or
more of the objects defined in the module are placed into the current
namespace."

In the following example, apperently the abovve claim is not true:
----------
# xx.py
X = 35
----------
# yy.py
from xx import X
import xx

print "BEFORE, X =", X
print "BEFORE, xx.X =", xx.X
X = 99
print "AFTER, X =", X
print "AFTER, xx.X =", xx.X
----------

Running yy.py produces the result:

BEFORE, X = 35
BEFORE, xx.X = 35
AFTER, X = 99
AFTER, xx.X = 35

So, apparently the imported X is a copy of xx.X, not the same object by
another name.
Can anyone explain this result? Is this result justified by a Python
language specifcation?

- Parzival






More information about the Python-list mailing list