Greetings,
let assume that we have a an object k of type class A. We defined a second class B(A). What is the best practice to "convert" object k to class B and preserve all data in k?
Thanks,
Shakov
Greetings,
let assume that we have a an object k of type class A. We defined a second class B(A). What is the best practice to "convert" object k to class B and preserve all data in k?
Thanks,
Shakov
a = A() # parent class
b = B() # subclass
b.value = 3 # random setting of values
a.__dict__ = b.__dict__ # give object a b's values
# now proceed to use object a
Would this satisfy your use case? Note: Only the instance variables of b will be accessible from object a, not class B's class variables. Also, modifying variables in a will modify the variable in b, unless you do a deepcopy:
import copy
a.__dict__ = copy.deepcopy(b.__dict__)
You cannot type cast a baseclass instance to a subclass instance, so there's no way to for k to be of type B. What you can do is create an instance of B based on k, e.g.
k = A()
foo = B(k)
and you would copy all the interesting bits from k to foo in B's __init__() method, possibly everything with BrainCore's self.__dict__ = k.__dict__ method (just make sure you do a deep copy if necessary).
The question is why do you have to do this? Can't you just create an instance of B instead of an instance of A to begin with?
This does the "class conversion" but it is subject to colateral damage. Creating another object and replacing its dict as BrainCore posted would be safer - but this code does what you asked, with no new object being created.
class A(object):
pass
class B(A):
def __add__(self, other):
return self.value + other
a = A()
a.value = 5
a.__class__ = B
print a + 10