Can you create an instance of a subclass with an existing instance of the base class?

Cavingdeep cavingdeep at gmail.com
Sat Apr 22 00:56:12 EDT 2006


With new-style classes you can find out a class' subclasses and then
you can instantiate the subclass you want. Suppose you have two classes
A and B, B is a subclass of A, A is a new-style class. Now you have an
A's instance called "a", to instance B you can do the following:

b = a.__class__.__subclasses__()[0]()




More information about the Python-list mailing list