Entry
I update a class with 'reload (module)'. How can I update the instances too?
How to change class instances behavior via reload?
Jun 27th, 2000 00:55
unknown unknown, Laurent Szyster, Jürgen Hermann
You'd have to find the instance one-by-one. If you manage to get hold
of them, you can set their __class__ attribute to the new class:
>>> class A:
... def doit(self):print "old"
...
>>> a=A()
>>> class A:
... def doit(self):print "new"
...
>>> a.doit()
old
>>> a.__class__=A
>>> a.doit()
new
--- Alternate ---
You should implement static and dynamic features that you need in
different modules and use proxy methods.
Like this
-- dtest.py --
def method(self):
...
-- test.py --
class test:
def method(self):
return dtest.method(self)
And then,
>>> import test, dtest
>>> t = test.test()
>>> reload(dtest)
>>> t.method()
----------------------
Laurent showed you how to do this automatically. Another technique I
used to DETECT whether the implementation of a class has changed is
this:
class spam:
def __init__(self):
pass
def checkReload(self):
return self.__class__ != spam