[Python-Dev] unexpected reload() behavior (original) (raw)

Guido van Rossum guido at python.org
Sat Mar 20 16:29:35 EST 2004


Not believing that old objects remained after the reload() I wrote a short test:

a = 5 b = 7 c = (1,2,3) imported it, modified it to a = 9 c = (1,2,3) then reloaded it. I was surprised to find that reloadtst.b did indeed still exist: >>> import reloadtst >>> dir(reloadtst) >>> dir(reloadtst) ['builtins', 'doc', 'file', 'name', 'a', 'b', 'c'] >>> # edit reloadtst.py ... >>> reload(reloadtst) <module 'reloadtst' from 'reloadtst.py'> >>> dir(reloadtst) ['builtins', 'doc', 'file', 'name', 'a', 'b', 'c'] It seems counterintuitive to me that reloadtst.b should still be defined. Is that behavior intention or accidental?

Intentional. A module's dict is not emptied when the reloaded module is executed. This allows code like this (which I have written) that preserves a cache across relaod() calls:

try:
    cache
except NameError:
    cache = {}

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list