I have a set of classes where a child is derived from a parent. What I am trying to achieve is to create an object of the parent class that gets all its values from an object of the child class. The only way I found is this:
from copy import deepcopy
class Parent(object):
def __init__(self, value):
self.val = value
class Child(Parent):
def __init__(self, val=8):
super(Child, self).__init__(val)
chval=5
par=Parent(3)
ch=Child()
parent= Parent(4)
parent.__dict__ = deepcopy(super(Child, ch).__dict__)
print(parent.val)
print(type(par), type(ch), type(parent))
The output is indeed
8
(<class '__main__.Parent'>, <class '__main__.Child'>, <class '__main__.Parent'>)
but I am not sure whether this is a good, pythonesque and risk-free method of doing this
chvaldoes not do anything)