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

后端 未结 15 2601
無奈伤痛
無奈伤痛 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:52

    There's a super() in Python too. It's a bit wonky, because of Python's old- and new-style classes, but is quite commonly used e.g. in constructors:

    class Foo(Bar):
        def __init__(self):
            super(Foo, self).__init__()
            self.baz = 5
    

提交回复
热议问题