How to change class instances behavior via reload?

Dirk-Ulrich Heise hei at adtranzsig.de
Mon Jun 26 07:46:20 EDT 2000


I don't know if my suggestion solves your problem.
My problem was: I wanted to write an interactive
program in an embedded Python. When testing the
behaviour of its objects, i would discover a slight
malfunction in one of the existing objects.
So i would like to fix the erroneous method and
reload it, without destroying the living objects.

I solved this via an editor macro that would create
a temp .PY module for the method i fixed that looks
like this:

# explanation of the identifiers i use:
# originalmodule - name of .PY file where your class is defined in
# Method - name of the method that needs to be reloaded in a
#    fixed version
# tempmodule - name of temporary .PY file created
# Classname - name of the class in originalmodule.py
#   the Method is a member of

# import everything from the original module the
# method comes from:
import originalmodule
from originalmodule import *
# now a copy of my fixed version of the method:
# (notice that it's unindented now!)
def Method(self,bla):
  self.x = bla
     # example code
# eof

After saving this temp PY file (say, as tempmodule.py),
the following is done
in the Python interpreter:

del tempmodule
  # in case it already existed as an imported module
import tempmodule
reload(tempmodule)
  # don't ask me why i do this... maybe unnecessary
  # - my editor macro just does it this way
  # Attention, originalmodule must exist (already be imported)
  # in the main namespace for the following to work:
setattr(originalmodule.Classname,"Method",tempmodule.Method)


This results in modified behaviour for all living instances of
the class, and it proved to supply a real development boost
for me. I don't know if this answers your question, but
it's an interesting technique anyway. And Python is the only
OO-language i know of that provides this possibility.


(Your approach would create a second instance of the whole
module, as older objects still refer to the code in the old
module instance so it can't be deleted)
--
Dipl.Inform. Dirk-Ulrich Heise
hei at adtranzsig.de
dheise at debitel.net


"Jerome Quelin" <jerome.quelin at insalien.org> schrieb im Newsbeitrag
news:961962342.297785615 at news.libertysurf.fr...
> 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