Hope this isn't too basic, but...
Suppose I have a base class pa(), and I have derived classes pai(), paj(), etc, that inherit from the base class.
I would like to instantiate an object from base class pa():
>>> from pamod import *
>>> mypa = pa()
>>> mypa
<pamod.pa object at 0x28d4fd0>
>>>
... and then promote it (cast it) to a derived class, based on some action:
>>> mypa.do(this)
>>> mypa
<pamod.pai object at 0x28d4fd0>
>>>
Where based on the value of "this", mypa becomes an object from class pai, etc.
I know I can assign to the object's __class__
.
mypa.
__class__
= pai
However I'm wondering if that's really the best approach.
Under my scheme, each instance of pai (say) would have started life as an instance of the base class pa, so pai's __init__
method could be overloaded somehow, I'm guessing.
See this discussion.
Peter