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

Alex Martelli aleaxit at yahoo.com
Thu Mar 1 04:05:48 EST 2001


"Parzival Herzog" <parz at home.com> wrote in message
news:ifmn6.6635$hn5.857862 at news1.rdc1.mb.home.com...
> In the Essential Python Reference, it states (p. 72):
>
> "The from statement is identical to import, except 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."

Right.  I.e., it's identical, except that it isn't:-).

> # yy.py
> from xx import X
> import xx

Note that this *PAIR* of statements IS identical in effect to the following:

import xx
X = xx.X

Is this semantical identity clear to you...?  We're doing two bindings in
the current module's namespace: we bind a name 'xx' to a module object
(the same as sys.modules['xx'], though we'd need an import sys to check);
also, we bind a name 'X' (again, in the current module's namespace) to
whatever object name 'X' was bound to in the xx module's namespace.

All right so far?

> print "BEFORE, X =", X
> print "BEFORE, xx.X =", xx.X

And you might also say
    print "Before, are they the same...?", X is xx.X
and get a nice 1 result from the 'is' operator (which does not prove
much when the object being referred to is immutable -- specifically,
a small integer -- but you could try with more 'elaborate' objects and
still pass the 'is' test; X and xx.X *MUST* refer to the SAME object
here, whatever that object might be).

Still OK...?

> X = 99

This *RE-BINDS* name X (in the current namespace) to something else.
This, *as usual*, does *NOT* affect any other binding that might be
in effect to the object that this name was bound to before the
re-binding.

Side-note with explanatory purposes, which you may want to skip
except for the first 6 lines (the rest are for completeness)...:

Binding affects names; specifically, it (only!) changes what object
that name refers to (until further notice, e.g. another rebinding of
the same name).  It does *NOT* affect the object being bound to; in
fact, the object need not even be aware of how it's being referred to,
in one or more ways, from whatever number of places -- bindings,
rebindings, unbindings, names, slots in lists, entries in dictionaries
(which is what names basically boil down to), whatever  -- _except that _:
  if the _number_ of such bindings to the object falls to zero, the object
  may be destroyed (it's immediately destroyed when that happens, in the
  current version of CPython, but that's an implementation artefact --
  Jython, and possibly future CPython's [and other implementations yet,
  such as Python.NET], are more 'relaxed' about such time-of-destruction
  issues; also, it's not *necessary* for the number of bindings to fall
  to zero in order for object-destruction to be triggered -- Jython and
  .NET, and under some conditions CPython 2.0 and higher, are also able
  to destroy objects caught in circular-reference loops, whose numbers of
  bindings never falls to zero but which still become 'unreachable').

'import' has very little to do with what is happening here.  Through
*whatever* mechanism a name was bound to a value, re-binding the name
does not affect the value, nor does it affect other bindings to the
same value.


Alex






More information about the Python-list mailing list