Overriding a parent class's methods

orokusaki picture orokusaki · Sep 27, 2010 · Viewed 13.5k times · Source

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?

Answer

Manoj Govindan picture Manoj Govindan · Sep 27, 2010

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.