【发布时间】:2011-04-17 14:52:56
【问题描述】:
我看到人们一直在做的事情是:
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.
这是因为我和所有错误的程序员在一起,还是有充分的理由?
【问题讨论】:
-
ExcitingMan.say_hi()的返回值对于大多数处理器来说通常过于令人兴奋。
标签: python class methods subclass overriding