Reloading a module
Mark Jackson
mjackson at alumni.caltech.edu
Tue Oct 1 07:52:58 EDT 2002
Tom <bondpaper at earthlink.net> writes:
> I wish it were that simple. I've used reload(module) many times, each
> time hoping that it would do what the documentation says it will do.
See the language reference on reload, at
http://www.python.org/doc/current/lib/built-in-funcs.html, in
particular:
When a module is reloaded, its dictionary (containing the module's
global variables) is retained. Redefinitions of names will override the
old definitions, so this is generally not a problem. If the new version
of a module does not define a name that was defined by the old version,
the old definition remains. This feature can be used to the module's
advantage if it maintains a global table or cache of objects -- with a
try statement it can test for the table's presence and skip its
initialization if desired.
If you want to remove a name and test the result with reload: don't
remove the defining line, just follow it immediately with del.
--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
How can anyone be expected to govern a country with two
hundred and forty-six cheeses? - Charles de Gaulle
More information about the Python-list
mailing list