Changing the class of an instance

Alex cut_me_out at hotmail.com
Sat Jul 15 15:05:36 EDT 2000


>    Well it's exactly what I was doing once because I was a lot more
> clever than the guys who said I shouldn't. And then I decided quite
> suddenly that they were right. (At the end of a _long_ stretch trying
> to figure out why the heck something wasn't working - of course the
> answer is the object in question was very different from the object it
> appeared it should be...)

It can have really confusing consequences, but it's still too useful for
me to abandon.  If you have a class which takes a long time to
instantiate (e.g., you have to load a lot of statistical data into
memory,) then having a script like

class C:
    pass

if 't' not in dir():
    t = C()

t.__class__ = C
t.experiment_with_instance()

can really decrease your turn-around time when you're debugging -- you
can change t's methods around behind its back without changing its data.

Alex.



More information about the Python-list mailing list