了解这一点的关键是super()
是用于实现协同继承。班级如何合作取决于程序员。 super()
是不是魔术,并不知道你想要什么!对于不需要协作继承的扁平层次结构使用super并没有多大意义,所以在这种情况下,S. Lott的建议就显现出来了。有用的5子类或可能不希望使用super()
根据自己的目标:)
例如:摘要答:< - B,但你要支持C的插入,就像这样< - Visual C < - B 。
class A(object):
"""I am an abstract abstraction :)"""
def foo(self):
raise NotImplementedError('I need to be implemented!')
class B(A):
"""I want to implement A"""
def foo(self):
print('B: foo')
# MRO Stops here, unless super is not A
position = self.__class__.__mro__.index
if not position(B) + 1 == position(A):
super().foo()
b = B()
b.foo()
class C(A):
"""I want to modify B and all its siblings (see below)"""
def foo(self):
print('C: foo')
# MRO Stops here, unless super is not A
position = self.__class__.__mro__.index
if not position(C) + 1 == position(A):
super().foo()
print('')
print('B: Old __base__ and __mro__:\n')
print('Base:', B.__bases__)
print('MRO:', B.__mro__)
print('')
# __mro__ change implementation
B.__bases__ = (C,)
print('B: New __base__ and __mro__:\n')
print('Base:', B.__bases__)
print('MRO:', B.__mro__)
print('')
b.foo()
和输出:
B: foo
B: Old __base__ and __mro__:
Base: (<class '__main__.A'>,)
MRO: (<class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
B: New __base__ and __mro__:
Base: (<class '__main__.C'>,)
MRO: (<class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
B: foo
C: foo
这使得很少的感觉。你应该知道哪些超类方法被实现了(为什么`super`有意义),哪些没有实现,因为它们是抽象的。你可以阅读来源。 – 2011-01-25 22:09:27
`raise SyntaxError`也在语言中。问题是“为什么在抽象类的简单检查可以节省编写所有代码时编写所有代码”? – 2011-01-25 22:15:58
@ S.洛特啊,现在明白了。顺便说一句,你应该提交这个答案,因为它是。 – gotgenes 2011-01-25 22:16:44