Reloading a module
Tom
bondpaper at earthlink.net
Tue Oct 1 00:58:46 EDT 2002
In article <mailman.1033441502.25759.python-list at python.org>,
Jeff Epler <jepler at unpythonic.net> wrote:
>
> As for .pyc vs .py, Python follows these steps when importing a module:
> if a .pyc file exists, read the magic number, version, and the
> stored timestamp. Compare the version to this Python version,
> and the stored timestamp to the .py file's timestamp. If these
> both match, or the version matches and the .py file doesn't exist,
> use the .pyc file.
> otherwise, compile the .py file and rebuild the .pyc file along the way
>
> The only circumstance that would interfere with this is NFS or another
> network filesystem making the check of the timestamp on the .py file
> unreliable---i.e., python can fail to detect that the .py file has been
> updated, because the OS gives it stale data. Using a filesystem that has
> no timestamp at all (or a very granular timestamp, so that two revisions of
> "silly.py" could share the same timestamp) could cause the same problem,
> though I don't know of any such filesystems offhand.
>
> (That said, I rarely actually notice problems of this type over NFS)
>
> It's possible that you've discovered some sort of new problem with the way
> .pyc caching or reloading work, but the idea is so simple that I can't see it
> failing...
>
> Jeff
>
Jeff,
Try this:
Here's a file called randomclass.py:
class randomClass:
def printit(self):
print "This is rather silly"
myRandomValue = "Foo"
>>>import randomclass
>>> dir(randomclass)
['__builtins__', '__doc__', '__file__', '__name__', 'myRandomValue',
'randomClass']
>>>m = randomclass
>>>print m.myRandomValue
Foo
>>> n = randomclass
>>> n.randomClass().printit()
This is rather silly
Now change the file by removing the line:
myRandomValue = "Foo"
...and then save the change.
>>> reload(randomclass)
<module 'randomclass' from 'randomclass.py'>
>>> dir(randomclass)
['__builtins__', '__doc__', '__file__', '__name__', 'myRandomValue',
'randomClass']
Odd...the reference to myRandomValue is still there, and it behaves as
though the modification was never made:
>>> n = randomclass
>>> print n.myRandomValue
Foo
It doesn't seem like this should be happening. It happens the same on
both Linux and the Mac (os 9).
Regards,
Tom
More information about the Python-list
mailing list