How to change class instances behavior via reload?

Jerome Quelin jerome.quelin at insalien.org
Sun Jun 25 15:45:42 EDT 2000


Hi all,

I would like to change the behavior of all the existing instances of a
class when using the reload statement. I wonder if this is possible?

Consider the following example:

-- file test.py
class Test:
    def func(self):
        print "first version"
-- end file
>>> import test
>>> t1=test.Test()
>>> t1.func()
first version
>>>
-- Editing file test.py, without leaving interpreter
class Test:
    def func(self):
        print "second version"
--
>>> reload(test)
<module 'test' from 'test.py'>
>>> t2=test.Test()
>>> t2.func()
second version
>>> t1.func()
first version
>>>

This seems correct, because t1 holds a reference to the code of the
first version of test.py. But when reloading, I would like to change
the behavior of all existing instances of the class Test, and not only
the behavior of the new instances that I will create. That is, I want
t1.func() to output "second version". I wonder if this is possible and
how?

Thanks,
Jerome
--
jerome.quelin at insalien.org



More information about the Python-list mailing list