+ 3
Is this the correct way to inherit y? Also how do i make B inherit a method like sum()?
3 odpowiedzi
+ 3
#Yes
class A:
def __init__(self, x, y, f) :
self.x = x
self.y = y
self.f = f
def sum(self) :
return self.x + self.y + self.f
class B(A) :
def __init__(self,y): # correction
super().__init__(y,y,y)
print(B(5).sum())
+ 3
Yes.
You have __inti__(self, y) so need one argument for y.
And it is passed to super class init, creates an object of B class. Now you can access sum() of A class, by inheritance, which returns 5+5+5
hope it helps..
+ 1
Jayakrishna🇮🇳 Whats 5? Is it for y?