views:

54

answers:

3

Something that I see people doing all the time is:

class Man(object):
    def say_hi(self):
            print('Hello, World.')

class ExcitingMan(Man):
    def say_hi(self):
            print('Wow!')
            super(ExcitingMan, self).say_hi()  # Calling the parent version once done with custom stuff.

Something that I never see people doing is:

class Man(object):
    def say_hi(self):
            print('Hello, World.')

class ExcitingMan(Man):
    def say_hi(self):
            print('Wow!')
            return super(ExcitingMan, self).say_hi()  # Returning the value of the call, so as to fulfill the parent class's contract.

Is this because I hang with all the wrong programmers, or is it for a good reason?

+1  A: 

I would suspect that, at least in this case, because the parent function doesn't return anything, there is no point in returning its result. In general though, when the parent function does return something, I think it's a fine practice.

Really, whether or not you return what the super class does depends on exactly what you want the code to do. Do it if it's what you want and what's appropriate.

JoshD
+2  A: 

I'd argue that explicitly returning the return value of the super class method is more prudent (except in the rare case where the child wants to suppress it). Especially when you don't know what exactly super is doing. Agreed, in Python you can usually look up the super class method and find out what it does, but still.

Of course the people who wrote the other version might have written the parent class themselves and/or known that it has no return value. In that case they'd have decided to do without and explicit return statement.

Manoj Govindan
+2  A: 

In the example you give the parent class method has no explicit return statement and so is returning None. So in this one case there's no immediate issue. But note that should someone modify the parent to return a value we now probably need to modify each child.

I think your suggestion is correct.

djna