Deprecating reload() ???
tjreedy at udel.edu
Sun Mar 14 08:51:13 CET 2004
"David MacQuigg" <dmq at gain.com> wrote in message
news:6kk6501shlve52ds2rjdopa73jhpchprg3 at 4ax.com...
> Just to make sure I understand this, I think what you are saying is
> that if I have a module M1 that defines a value x = 3.1, it will be
> impossible to keep track of the number of references to M1.x because
> the object '3.1' may have other references to it from other modules
> which use the same constant 3.1. This really does make it impossible
> to do a complete reload.
Currently, this is possible but not actual for floats, but it is actual, in
CPython, for some ints and strings. For a fresh 2.2.1 interpreter
> Warning: References to objects in the old module still exist:
> creating direct (not fully-qualified) references to objects within any
> module that is likely to be reloaded, and be assured that we will get
> a warning if we miss one.
A module is a namespace created by external code, resulting in a namespace
with a few special attributes like __file__, __name__, and __doc__. A
namespace contains names, or if you will, name bindings. It does not,
properly speaking, contain objects -- which are in a separate, anonymous
data space. One can say, reasonably, that functions and classes defined in
a module 'belong' to that module, and one could, potentially, track down
and replace all references to such.
As you have already noticed, you can make this easier by always accessing
the functions and classess via the module (mod.fun(), mod.clas(), etc.) --
which mean no anonymous references via tuple, list, or dict slots, etc.
However, there is still the problem of instances and their __class__
attribute. One could, I believe (without trying it) give each class in a
module an __instances__ list that is updated by each call to __init__.
Then super_reload() could grab the instances lists, do a normal reload, and
then update the __instances__ attributes of the reloaded classes and the
__class__ attributes of the instances on the lists. In other words,
manually rebind instances to new classes and vice versa.
Another possibility (also untried) might be to reimport the module as
'temp' (for instance) and then manully replace, in the original module,
each of the function objects and other constants and manually update each
of the class objects. Then instance __class__ attributes would remain
Either method is obviously restricted to modules given special treatment
and planning. Either update process also needs to be 'atomic' from the
viewpoint of Python code. A switch to another Python thread that accesses
the module in the middle of the process would not be good. There might
also be other dependencies I am forgetting, but the above should be a
Terry J. Reedy
More information about the Python-list