invoking a method from two superclasses

Carl Banks pavlovevidence at gmail.com
Tue Jun 30 21:23:11 EDT 2009


On Jun 30, 5:34 pm, Mitchell L Model <MLMLi... at Comcast.net> wrote:
> Allow me to add to my previous question that certainly the superclass
> methods can be called explicitly without resorting to super(), e.g.:
>
>     class C(A, B):
>         def __init__(self):
>             A.__init__(self)
>             B.__init__(self)
>
> My question is really whether there is any way of getting around the
> explicit class names by using super()


Yes there is: just make sure that all subclasses also call super.


    class A:
        def __init__(self):
            super().__init__()
            print('A')

    class B:
        def __init__(self):
            super().__init__()
            print('B')

    class C(A, B):
        def __init__(self):
            super().__init__()
            print('C')


Bam, that's it.

What's happening is that A's super calls B.  That is likely to seem
wrong to someone who is very familiar with OOP, but it's how Python's
MI works.

Read this essay/rant that explains how super works and why the author
thinks it's not useful.  Then ignore the last part, because I and many
others have found it very useful, desipte its drawbacks.

http://fuhm.net/super-harmful/


Carl Banks



More information about the Python-list mailing list