replacing the class of an instance

David Ascher da at ski.org
Fri Sep 24 16:51:54 EDT 1999


On Fri, 24 Sep 1999, Bill Janssen wrote:

> I'd like to be able to replace the class of previously created
> instances when I create a new version of the class.  Is it sufficient
> to keep track of all the instances, then go through and change the
> __class__ member of each instance when I change the class definition?
> 
> Or is there an even easier way?

Depending on your needs, you may be able to rip out the guts (aka the
methods and class attributes) of the class that the instances all share
and squeeze in the new class as a base class of the original class.  That
would avoid having to deal with circular references.

Something like (untested):

  orig_class.__dict__.clear()
  orig_class.__bases__ = (new_class, )

--david ascher





More information about the Python-list mailing list