Python's Multiple Inheritance: Picking which super() to call

后端 未结 3 773
野趣味
野趣味 2020-12-01 05:05

In Python, how do I pick which Parent\'s method to call? Say I want to call the parent ASDF2\'s __init__ method. Seems like I have to specify ASDF1

3条回答
  •  -上瘾入骨i
    2020-12-01 05:51

    super calls the next method in the method resolution order. In a linear inheritance tree, that will be method from the immediately parent class.

    Here, you have three parents, and the next __init__ method from ASDF1's perspective is that of ASDF2. In general, the safe thing to do is to pass the first class in the inheritance list to super unless you know why you want to do something else.

    The point of all of this is to relieve you of having to explicitly pick which __init__ methods to call. The question here is why you don't want to call all of the superclass __init__ methods.

    There's a fairly substantial literature on the use of super in python, and I recommend that you google the topic, and read what results.

提交回复
热议问题