import a modified module

David MacQuigg dmq at gain.com
Fri Mar 26 17:43:09 EST 2004


On 26 Mar 2004 20:03:45 GMT, mjackson at alumni.caltech.edu (Mark
Jackson) wrote:

>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."

I agree the second example is a little too complex.  I might move this
to the exercises section, and replace it with an example Mel Wilson
contributed on this mailing list 3-20-04. This example shows the
module contents before and after a reload, which is very enlightening.

I don't understand why the term "references" is suboptimal.  A module
M1 has a namespace, which is detailed in a dictionary M1.__dict__.
This dictionary contains _references_ to objects in memory (not the
objects themselves).  What terminology would be better?

>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.

This could be an example in the "Gotchas" section.  What's happening
here is that the a.append(3) statement is changing the memory object
in place.  The references to that object (a and M1.a) are not changed,
and unlike the a = [1,2] statement, no new object is created.

You can verify this with:
>>> a = [1,2]
>>> id(a)
11025840
>>> a.append(3)
>>> a
[1, 2, 3]
>>> id(a)
11025840
>>> 

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

Import is covered very thoroughly in six pages at the end of the
chapter just before this.  I've written a short summary, which I will
also hand out in the course.
'''
How Imports Work
(see the Python Language Reference, section 6.12 for details)

Statement forms:

import M1
from M1 import x, y
M1 = __import__('M1')

Procedure:

1. Search for module M1 in the module registry (sys.modules
dictionary)
   If found GOTO 7.

2. Search for M1 in the directories specified in sys.path:
  a. The directory of the current file
  b. The directories in PYTHONPATH
  c. The standard library directories
  d. The directories specified in any .pth files found in the previous
     steps

3. Load M1 into memory, compiling if necessary.

4. Create a new module object with an empty namespace.

5. Add M1 to the module registry.

6. Execute the M1 code in the new module's namespace.

7. Add M1 ( or x and y ) to the local namespace of the scope where the
import statement occurs.
'''

Your feedback is appreciated.

-- Dave





More information about the Python-list mailing list