I'm trying to use a parent method from a child class. A bare bone example is given below.
class one:
def multiply(self, x, y):
self.x = 500
return self.x*y
class two(one):
def multiplier(self, x, y):
self.x = x
ver1 = one.multiply(one, self.x, y) # here, if I don't pass one as an argument, I get a TypeError
print('The answer of ver1:', ver1)
print('What is self.x btw?', self.x)
ver2 = super().multiply(self.x, y)
print('The answer of ver2:', ver2)
print('What is self.x now?', self.x)
t = two()
t.multiplier(3,4)
This prints:
The answer of ver1: 2000
What is self.x btw? 3
The answer of ver2: 2000
What is self.x now? 500
I looked on here and many answers seem to imply that ver2
is the correct way to call a parent method but I don't want self.x
to change in the child class, so the answer I want is ver1
. But, in ver1
, it seems redundant to pass one
as an argument when it is already specified that multiply
is one
's method (if I don't pass one
as an argument, I get
TypeError: multiply() missing 1 required positional argument: 'y'
So what is the correct way to call a method from a parent class without changing variables in the child class?