Changing the class of an instance

Vladimir Marangozov Vladimir.Marangozov at inrialpes.fr
Wed Jul 12 20:24:35 EDT 2000


Richard Tobin wrote:
> 
> Is it legal (it seems to work) to change the class of an object by
> assigning to its __class__ attribute?  In particular for the case where
> the new class is a subclass of the old.
> 
> I'm not looking for an argument (not even the five minute one) about
> whether it's a good idea, just whether it's legal.

Yes, it's perfectly legal. However, as the other replies are trying to
warn you, it is assumed that you know perfectly what this means.

Whenever an instance changes its __class__ from A to B, the instance
may change almost all of its properties, except its id and local instance
variables. IOW, it may change completely its behavior, for instance when
A and B have nothing in common. It's like moving from UK to Japan by
keeping your pounds in your pocket.

which-is-perfectly-legal-too'ly y'rs
-- 
       Vladimir MARANGOZOV          | Vladimir.Marangozov at inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252



More information about the Python-list mailing list