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?
There must be an inheritance connection between classes. All the abstract methods in the parent class should be overridden in the child class. If it declared the methods as static or final, then those methods cannot be overridden.
What Does Overriding Mean? Overriding is an object-oriented programming feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes.
If a method cannot be inherited, then it cannot be overridden. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.
If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With