[Python-Dev] Changing existing class instances
Guido van Rossum
guido@CNRI.Reston.VA.US
Wed, 19 Jan 2000 23:41:29 -0500
> Currently, when you replace a class definition with an updated
> version, it's really difficult to change existing class instances;
> you'd have to essentially sweep every Python object and check if it's
> an instance, starting at roots such as __main__ and sys.modules. This
> makes developing code in a long-running process difficult, Zope being
> the best example of this. When you modify a class definition used by
> Zope code, you can't update existing instances floating around in
> memory.
There might be another solution. When you reload a module, the module
object and its dictionary are reused.
Perhaps class and function objects could similarly be reused? It
would mean that a class or def statement looks for an existing object
with the same name and type, and overwrites that. Voila, all
references are automatically updated.
This is more work (e.g. for classes, a new bytecode may have to be
invented because the class creation process must be done differently)
but it's much less of a hack, and I think it would be more reliable.
(Even though it alters borderline semantics a bit.)
(Your extra indirection also slows things down, although I don't know
by how much -- not just the extra memory reference but also less
locality of reference so more cache hits.)
--Guido van Rossum (home page: http://www.python.org/~guido/)