import a modified module

Mark Jackson mjackson at alumni.caltech.edu
Fri Mar 26 15:03:45 EST 2004


David MacQuigg <dmq at gain.com> writes:

> The docs on reload are a little confusing.  I've written a piece at
> http://ece.arizona.edu/~edatools/Python/Reload.htm
> 
> Your comments are welcome.

Hm.  Your second ("more detailed") example is visually confusing, and
matters aren't helped by your use of what I would call suboptimal (or
at least nonstandard-for-Python) terminology such as "references."
This leads you into at least technical error, e.g.

    Perhaps the most confusing thing about reload is that it changes
    only references in its own namespace (with names like "M1.a" above).
    It does not change references outside this namespace (with
    names like "a" above ).

An admittedly pathological counterexample:

    >>> import M1
    >>> from M1 import a, b
    >>> print M1.a, M1.b
    [1, 2] abc
    >>> print a, b
    [1, 2] abc
    >>> reload(M1)
    <module 'M1' from 'M1.py'>
    >>> print M1.a, M1.b
    [1, 2, 3] ABC
    >>> print a, b
    [1, 2, 3] abc

How did a (in the __main__ namespace) track a (in the M1 namespace)?
Easy - in editing M1.py I replaced:

    a = [1, 2]

with:

    a.append(3)

Of course that *is* pathological - as is, M1.py cannot be freshly
imported because a would have no initial binding.  But I think in
explaining reload() one should strive for a formulation under which
this outcome would not be surprising.

(Really, to have a hope of understanding reload one first has to
understand import.)

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
    The supreme misfortune is when theory outstrips performance.
				- Leonardo da Vinci





More information about the Python-list mailing list