oro*_*aki 20 python methods overriding class subclass
我看到人们一直在做的事情是:
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.
我从未见过的人做的事情是:
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.
这是因为我和所有错误的程序员挂在一起,还是有充分的理由呢?
我认为显式返回超类方法的返回值更为谨慎(除非在极少数情况下,孩子想要抑制它).特别是当你不知道超级正在做什么的时候.同意,在Python中你通常可以查找超类方法并找出它的作用,但仍然如此.
当然,编写其他版本的人可能自己编写了父类,并且/或者知道它没有返回值.在那种情况下,他们决定不做和明确的退货声明.
在示例中,您给出的父类方法没有显式的return语句,因此返回None.所以在这一案例中没有立竿见影的问题.但请注意,如果有人修改父级以返回值,我们现在可能需要修改每个子级.
我认为你的建议是正确的.