Reputation: 71
I would like to have each class to have different or same variables and inherit with final class as below
class a(object):
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
class b(object):
def __init__(self, u, v):
self.u = u
self.v = v
def mul(self):
return self.u*self.v
class c(a, b):
def __init__(self,x,y,u,v,w):
super(c,self).__init__(x,y,u,v,w)
self.w = w
def div(self):
return (self.x+self.y+self.u+self.v)/self.w
Error as:
ob = c(1,2,3,4,5)
Traceback (most recent call last):
File "<ipython-input-20-0c1a3250f4a1>", line 1, in <module>
ob = c(1,2,3,4,5)
File "<ipython-input-19-5734ec980138>", line 20, in __init__
super(c,self).__init__(x,y,u,v,w)
TypeError: __init__() takes 3 positional arguments but 6 were given
How to fix error?
Upvotes: 0
Views: 48
Reputation: 13498
I'm not sure if this is good practice but you can make it work by calling the __init__()
for each superclass separately.
class a(object):
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
class b(object):
def __init__(self, u, v):
self.u = u
self.v = v
def mul(self):
return self.u*self.v
class c(a, b):
def __init__(self,x,y,u,v,w):
a.__init__(self, x, y)
b.__init__(self, u, v)
self.w = w
def div(self):
return (self.x+self.y+self.u+self.v)/self.w
k = c(1, 2, 3, 4, 5)
print(k.div())
>>>2.0
print(k.add())
>>>3
print(k.mul())
>>>12
Upvotes: 1