Search code examples
pythonmultiple-inheritancesuper

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


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 in the super()..? And if I want to call ASDF3's __init__, then I must specify ASDF2?!

>>> class ASDF(ASDF1, ASDF2, ASDF3):
...     def __init__(self):
...         super(ASDF1, self).__init__()
            
>>> ASDF()
# ASDF2's __init__ happened

>>> class ASDF(ASDF1, ASDF2, ASDF3):
...     def __init__(self):
...         super(ASDF2, self).__init__()

>>> ASDF()
# ASDF3's __init__ happened

Seems bonkers to me. What am I doing wrong?


Solution

  • That's not what super() is for. Super basically picks one (or all) of its parents in a specific order. If you only want to call a single parent's method, do this

    class ASDF(ASDF1, ASDF2, ASDF3):
        def __init__(self):
            ASDF2.__init__(self)