I have instance a
of class A
, and I want to make it also an instance of class B
(which inherits from A
).
I have tried this, but is is not behaving as expected:
class A:
def __init__(self):
self.a = 1
class B(A):
def __init__(self):
self.b = 2
a = A()
isinstance(a, A) # True
B.__init__(a)
isinstance(a, B) # False, I wished it was True...
I have seen this answer but I think my problem is a bit more general: here only the methods from B
will apply to a
, but I want a
to be a full-fledged instance of B
, i.e. also with member variables etc (as though a
had been directly created as an instance of B
, but with the inheritance from A
in a state exactly similar to a
).
How can I do this?? I don't mind creating a new instance if necessary, as I long as I can restore the exact same state in the end.
EDIT: I have also seen answers to this question, but they either only create a new instance of B
and retrieve one field from a
(that will be hard to extend to complicated A
classes with many attributes) or just set a.__class__ = B
which does not create attributes of B
.