tags:

views:

581

answers:

6

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().

+1  A: 

Yes, just stick to keyword arguments in your __init__ methods and you shouldn't have too many problems.

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

Casey
+5  A: 

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.

e-satis
*"If the feature exists, there is a reason."* This argument is simply untrue. Many languages have features that should not be used by anyone ever.
Mike Graham
+1  A: 

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.

juanjux
unless you're not using python 3 yet (which is the majority of python uses)
nosklo
What do you mean? super() is on Python 2 too.
juanjux
It's been backported I believe.
ilya n.
+6  A: 

the Book Expert Python Programming has discuss this topic: "super Pitfalls" section of Chapter 3. The book 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. So Copy several sentences again:
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.

sunqiang
+2  A: 

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, but of course there are legitimate use cases.

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 now the parent class name). In Python 3+ super is more convenient, so there one should definitely use super.

nikow
+1  A: 

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.

ilya n.