Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

python - The proper way to call a parent method from a child class

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?

question from:https://stackoverflow.com/questions/65925413/the-proper-way-to-call-a-parent-method-from-a-child-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use self, not one:

class two(one):
    def multiplier(self, x, y):
        self.x = x
        
        ver1 = self.multiply(x, y)
        print('The answer of ver1:', ver1)
        print('What is self.x btw?', self.x)

Super is useful if you override the same method but want access to the parent:

class two(one):
    def multiply(self, x, y):
        self.x = x
        
        ver2 = super().multiply(x, y)
        print('The answer of ver2:', ver2)
        print('What is self.x now?', self.x)

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')

This is because when you use self, the method is bound to the instance and the instance is passed automatically as first parameter. When using one.multiply, the method is not bound and you need to pass it manually. But this is not the right way to go, as you intuited.

I don't want self.x to change in the child class

There are two classes and an instance which is an instance of both classes due to inheritance. x is an instance attribute, so it belongs to the instance, not to any of both classes. It can't change in parent and not child class or the opposite.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...