My code works but I'm not sure if this the correct way to implement it. I'm trying to use the base class "add" method in the derived class. super().__add__ returns a base class therefore I must somehow "convert" the class back to the derived type before returning it. Can you please take a look and tell me if there is a better/ nicer way to implement this?
class a:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"{self.x} {self.y}"
def __add__(self, other):
return a(self.x + other.x, self.y + other.y)
class b(a):
def __init__(self, x, y, z):
super().__init__(x, y)
self.z = z
def __repr__(self):
str = super().__repr__()
return str + f" {self.z}"
def __add__(self, other):
temp = super().__add__(other)
new_z = self.z + other.z
return b(temp.x, temp.y, new_z)
Thanks! LI