How to call a Parent Class's method from Child Class in Python?

后端 未结 15 2578
無奈伤痛
無奈伤痛 2020-11-22 10:14

When creating a simple object hierarchy in Python, I\'d like to be able to invoke methods of the parent class from a derived class. In Perl and Java, there is a keyword for

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 10:46

    Python 3 has a different and simpler syntax for calling parent method.

    If Foo class inherits from Bar, then from Bar.__init__ can be invoked from Foo via super().__init__():

    class Foo(Bar):
    
        def __init__(self, *args, **kwargs):
            # invoke Bar.__init__
            super().__init__(*args, **kwargs)
    

提交回复
热议问题