Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Follow along...:</p> <pre><code>&gt;&gt;&gt; class A(object): pass ... &gt;&gt;&gt; A.__mro__ (&lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) &gt;&gt;&gt; class B(A): pass ... &gt;&gt;&gt; B.__mro__ (&lt;class '__main__.B'&gt;, &lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) &gt;&gt;&gt; class C(A): pass ... &gt;&gt;&gt; C.__mro__ (&lt;class '__main__.C'&gt;, &lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) &gt;&gt;&gt; </code></pre> <p>As long as we have single inheritance, <code>__mro__</code> is just the tuple of: the class, its base, its base's base, and so on up to <code>object</code> (only works for new-style classes of course).</p> <p>Now, with <em>multiple</em> inheritance...:</p> <pre><code>&gt;&gt;&gt; class D(B, C): pass ... &gt;&gt;&gt; D.__mro__ (&lt;class '__main__.D'&gt;, &lt;class '__main__.B'&gt;, &lt;class '__main__.C'&gt;, &lt;class '__main__.A'&gt;, &lt;type 'object'&gt;) </code></pre> <p>...you also get the assurance that, in <code>__mro__</code>, no class is duplicated, and no class comes after its ancestors, save that classes that first enter at the same level of multiple inheritance (like B and C in this example) are in the <code>__mro__</code> left to right.</p> <p>Every attribute you get on a class's instance, not just methods, is conceptually looked up along the <code>__mro__</code>, so, if more than one class among the ancestors defines that name, this tells you where the attribute will be found -- in the first class in the <code>__mro__</code> that defines that name.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload