Question

Or should I just explicitly reference the superclasses whose methods I want to call?

It seems brittle to repeat the names of super classes when referencing their constructors, but this page http://fuhm.net/super-harmful/ makes some good arguments against using super().

Was it helpful?

Solution

The book Expert Python Programming has discussed the topic of "super pitfalls" in chapter 3. It is worth reading. Below is the book's conclusion:

Super usage has to be consistent: In a class hierarchy, super should be used everywhere or nowhere. Mixing super and classic calls is a confusing practice. People tend to avoid super, for their code to be more explicit.

Edit: Today I read this part of the book again. I'll copy some more sentences, since super usage is tricky:

  • Avoid multiple inheritance in your code.
  • Be consistent with its usage and don't mix new-style and old-style.
  • Check the class hierarchy before calling its methods in your subclass.

OTHER TIPS

You can use super, but as the article says, there are drawbacks. As long as you know them, there is no problem with using the feature. It's like people saying "use composition, not inheritance" or "never use global variables". If the feature exists, there is a reason. Just be sure to understand the why and the what and use them wisely.

I like super() more because it allows you to change the inherited class (for example when you're refactoring and add an intermediate class) without changing it on all the methods.

super() tries to solve for you the problem of multiple inheritance; it's hard to replicate its semantics and you certainly shouldn't create any new semantics unless you're completely sure.

For single inheritance, there's really no difference between

class X(Y):
    def func(self):
        Y.func(self)

and

class X(Y):
    def func(self):
        super().func()

so I guess that's just the question of taste.

The problem people have with super is more a problem of multiple inheritance. So it is a little unfair to blame super. Without super multiple inheritance is even worse. Michele Simionato nicely wrapped this up in his blog article on super:

On the other hand, one may wonder if all super warts aren't hints of some serious problem underlying. It may well be that the problem is not with super, nor with cooperative methods: the problem may be with multiple inheritance itself.

So the main lesson is that you should try to avoid multiple inheritance.

In the interest of consistency I always use super, even if for single inheritance it does not really matter (apart from the small advantage of not having to know the parent class name). In Python 3+ super is more convenient, so there one should definitely use super.

Yes, you should use super() over other methods. This is now the standard object inheritance model in Python 3.

Just stick to keyword arguments in your __init__ methods and you shouldn't have too many problems. Additionally you can use **kwargs to support additional parameters that are not defined in levels of the inheritance chain.

I agree that it is brittle, but no less so than using the name of the inherited class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top