Converting an instance to a subclass?

Terry Reedy tjreedy at udel.edu
Fri Feb 16 17:46:58 EST 2001


> Is there a way I can cast the instances of class A into instances of
> class B so I can use the additional methods

Is this what you want (with 1.5.2)?

>>> class a: pass
>>> class b(a): pass
>>> i = a()
>>> i.__class__
<class __main__.a at 7934f0>
>>> i.__class__ = b
>>> i.__class__
<class __main__.b at 7932b0>

> I can't find anything  about it in "Programming Python"

I believe that __class__ became writeable after that was written -- or it
may have been too esoteric (and dangerous) to include.

Terry J. Reedy






More information about the Python-list mailing list