13

Is this a legal use of super()?

class A(object):
    def method(self, arg):
        pass

class B(A):
    def method(self, arg):
        super(B,self).method(arg)

class C(B):
    def method(self, arg):
        super(B,self).method(arg)

Thank you.

upperBound
  • 680
  • 4
  • 11

2 Answers2

12

It will work, but it will probably confuse anyone trying to read your code (including you, unless you remember it specifically). Don't forget that if you want to call a method from a particular parent class, you can just do:

A.method(self, arg)
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • I assume, if there is no multiple-inheritance involved, both `A.method(self, arg)` and `super(B,self).method(arg)` are equivalent (minus the potential user confusion)? – upperBound May 25 '11 at 20:55
  • @upperBound: Yes. Although obviously in both cases you need to be aware of what happens if you change their inheritance relationships. – Thomas K May 25 '11 at 20:59
3

Well, "legal" is a questionable term here. The code will end up calling A.method, since the type given to super is excluded from the search. I would consider this usage of super flaky to say the least, since it will skip a member of the inheritance hierarchy (seemingly haphhazardly), which is inconsistent with what I would expect as a developer. Since users of super are already encouraged to maintain consistency, I'd recommend against this practice.

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
  • 1
    I agree. What do you think about using `A.method(self, arg)` since this too would step over the hierarchy? – upperBound May 25 '11 at 21:08