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

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

    If you don't know how many arguments you might get, and want to pass them all through to the child as well:

    class Foo(bar)
        def baz(self, arg, *args, **kwargs):
            # ... Do your thing
            return super(Foo, self).baz(arg, *args, **kwargs)
    

    (From: Python - Cleanest way to override __init__ where an optional kwarg must be used after the super() call?)

提交回复
热议问题