Force child class to call parent method when overriding it

后端 未结 7 1934
梦谈多话
梦谈多话 2020-12-15 16:05

I am curious whether there is a way in Python to force (from the Parent class) for a parent method to be called from a child class when it is being overridden.

Examp

7条回答
  •  轮回少年
    2020-12-15 16:54

    All you need is super.

    With it, your code could look like this:

    class Parent(object):
        def __init__(self):
            self.someValue = 1
    
        def start(self):
            self.someValue += 1
    
    
    class Child(Parent):
        def start(self):
            # put code here
            super().start()
            # or here
    

提交回复
热议问题