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

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

    Use the super() function:

    class Foo(Bar):
        def baz(self, arg):
            return super().baz(arg)
    

    For Python < 3, you must explicitly opt in to using new-style classes and use:

    class Foo(Bar):
        def baz(self, arg):
            return super(Foo, self).baz(arg)
    

提交回复
热议问题