<p>如果您控制了<code>A</code>和<code>B</code></strong>的源代码,那么无论是“新样式”还是“旧样式”都可以工作。否则,可能需要使用适配器类。</p>
<h3>可访问源代码:正确使用“new style”</h3>
<pre><code>class A(object):
def __init__(self):
print("-> A")
super(A, self).__init__()
print("<- A")
class B(object):
def __init__(self):
print("-> B")
super(B, self).__init__()
print("<- B")
class C(A, B):
def __init__(self):
print("-> C")
# Use super here, instead of explicit calls to __init__
super(C, self).__init__()
print("<- C")
</code></pre>
<pre><code>>>> C()
-> C
-> A
-> B
<- B
<- A
<- C
</code></pre>
<p>在这里,方法解析顺序(MRO)指示以下内容:</p>
<ul>
<li><code>C(A, B)</code>首先指示<code>A</code>,然后指示<code>B</code>。MRO是<code>C -> A -> B -> object</code>。</li>
<li><code>super(A, self).__init__()</code>沿着在<code>C.__init__</code>到<code>B.__init__</code>中启动的MRO链继续。</li>
<li><code>super(B, self).__init__()</code>沿着在<code>C.__init__</code>到<code>object.__init__</code>中启动的MRO链继续。</li>
</ul>
<p>你可以说这个病例是为多重遗传而设计的。</p>
<h3>可访问源代码:正确使用“旧样式”</h3>
<pre><code>class A(object):
def __init__(self):
print("-> A")
print("<- A")
class B(object):
def __init__(self):
print("-> B")
# Don't use super here.
print("<- B")
class C(A, B):
def __init__(self):
print("-> C")
A.__init__(self)
B.__init__(self)
print("<- C")
</code></pre>
<pre><code>>>> C()
-> C
-> A
<- A
-> B
<- B
<- C
</code></pre>
<p>这里,MRO并不重要,因为<code>A.__init__</code>和<code>B.__init__</code>是显式调用的。<code>class C(B, A):</code>也同样有效。</p>
<p>尽管这种情况并不像前一种情况那样为新样式中的多重继承而“设计”,但多重继承仍然是可能的。</p>
<hr/>
<p>现在,如果<code>A</code>和<code>B</code>来自第三方库,也就是说,<strong>您无法控制<code>A</code>和<code>B</code></strong>的源代码,该怎么办?简而言之:您必须设计一个实现必要的<code>super</code>调用的适配器类,然后使用一个空类来定义MRO(请参见<a href="https://rhettinger.wordpress.com/2011/05/26/super-considered-super/" rel="nofollow noreferrer">Raymond Hettinger's article on ^{<cd20>}</a>-特别是“如何合并非合作类”一节)。</p>
<h3>第三方父项:<code>A</code>不实现<code>super</code>;<code>B</code>不实现</h3>
<pre><code>class A(object):
def __init__(self):
print("-> A")
print("<- A")
class B(object):
def __init__(self):
print("-> B")
super(B, self).__init__()
print("<- B")
class Adapter(object):
def __init__(self):
print("-> C")
A.__init__(self)
super(Adapter, self).__init__()
print("<- C")
class C(Adapter, B):
pass
</code></pre>
<pre><code>>>> C()
-> C
-> A
<- A
-> B
<- B
<- C
</code></pre>
<p>类<code>Adapter</code>实现<code>super</code>,以便<code>C</code>可以定义MRO,它在执行<code>super(Adapter, self).__init__()</code>时起作用。</p>
<p>如果相反呢?</p>
<h3>第三方父项:<code>A</code>实现<code>super</code>;<code>B</code>不</h3>
<pre><code>class A(object):
def __init__(self):
print("-> A")
super(A, self).__init__()
print("<- A")
class B(object):
def __init__(self):
print("-> B")
print("<- B")
class Adapter(object):
def __init__(self):
print("-> C")
super(Adapter, self).__init__()
B.__init__(self)
print("<- C")
class C(Adapter, A):
pass
</code></pre>
<pre><code>>>> C()
-> C
-> A
<- A
-> B
<- B
<- C
</code></pre>
<p>这里的模式相同,只是执行顺序是在<code>Adapter.__init__</code>;<code>super</code>调用中切换的,然后是显式调用。请注意,对于第三方父级的每个情况,都需要一个唯一的适配器类。</p>
<blockquote>
<p>So it seems that unless I know/control the init's of the classes I inherit from (<code>A</code> and <code>B</code>) I cannot make a safe choice for the class I'm writing (<code>C</code>).</p>
</blockquote>
<p>尽管您可以通过使用适配器类来处理不控制源代码<code>A</code>和<code>B</code>的情况,但是您必须知道父类的in it是如何实现<code>super</code>(如果有的话)的。</p>