0

I have just read Method Resolution Order by GvR, but I wonder if the following statement holds true(I agree with this) in Python's Super is nifty, but you can't use it. So super() causes the next method in the MRO to be called? Also noted in this comment.

One big problem with 'super' is that it sounds like it will cause the superclass's copy of the method to be called. This is simply not the case, it causes the next method in the MRO to be called (...)

class A(object):
    def __init__(self):
        #super(A, self).__init__()
        print 'init A'

class B(object):
    def __init__(self):
        print 'init B'

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

c = C()

gives

init A
init C

While

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

class B(object):
    def __init__(self):
        print 'init B'

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

c = C()

gives

init B
init A
init C
Community
  • 1
  • 1
schemacs
  • 2,783
  • 8
  • 35
  • 53
  • 2
    So ... What's your question? – mgilson Dec 05 '13 at 07:08
  • 1
    super() causes the next method in the MRO to be called? – schemacs Dec 05 '13 at 07:09
  • Yes it does. Did you examine example in the article you linked, that is https://fuhm.net/super-harmful/example1-1.py? – alko Dec 05 '13 at 07:21
  • The mro in example1-2 is [E, C, A, D, B, object]. Both E and C's init method has one arg, but A's has no arg, so throw exception at A's init method(`__init__() takes exactly 2 arguments (1 given)`). – schemacs Dec 05 '13 at 07:30

1 Answers1

1

Looks like the expected results in both cases... In the first case C calls to A (next class in MRO) which prints "init A" and returns so flow comes back to C which prints "init C" and returns. Matches your output.

In the second case C calls A (next in MRO) which calls B (next to A in MRO) which prints "init B" and returns so flow comes back to A which prints "init A" and returns back to C which prints "init C".

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • yes, the `super` is so tricky as demonstrated in [Python’s super() considered super!](http://rhettinger.wordpress.com/2011/05/26/super-considered-super/). – schemacs Dec 05 '13 at 07:22
  • @schemacs: `super` is not tricky - it calls the next method in the mro, period. Multiple inheritance is tricky but that's another topic. – bruno desthuilliers Dec 05 '13 at 12:35