Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot inherit from multiple classes defining __slots__?
    text
    copied!<p>A certain situation in Python recently alarmed me, and its reason is still not completely clear after a little research. The following class definitions appear to working flawlessly and will produce what is intended:</p> <pre><code>class A: __slots__ = 'a', 'b' class B(A): __slots__ = () class C(A): __slots__ = () class D(B, C): __slots__ = () </code></pre> <p>There are four classes arranged in a diamond inheritance pattern. However, a somewhat similar pattern is not allowed. The following class definitions seem as though they are functioning the same as the first:</p> <pre><code>class B: __slots__ = 'a', 'b' class C: __slots__ = 'a', 'b' class D(B, C): __slots__ = () Traceback (most recent call last): File "&lt;pyshell#74&gt;", line 1, in &lt;module&gt; class D(B, C): __slots__ = () TypeError: multiple bases have instance lay-out conflict </code></pre> <p>However, a <code>TypeError</code> is raised in this example. So three questions arise: (1) Is this a bug in Python, considering the slot names? (2) What would justify such an answer? (3) <em>What is the best workaround?</em></p> <hr> <p><strong>References:</strong></p> <ul> <li><a href="http://mail.python.org/pipermail/python-list/2002-December/740850.html">__slots__ and multiple inheritance</a> (with replies following)</li> <li><a href="http://bytes.com/topic/python/answers/576995-multiple-inheritance-__slots__">Multiple inheritance and __slots__</a></li> <li><a href="https://groups.google.com/group/comp.lang.python/msg/0f2e859b9c002b28">Don't use __slots__</a></li> <li><a href="http://www.velocityreviews.com/forums/t339645-multiple-inheritance-__slots__-problem.html">Multiple Inheritance __slots__ problem</a></li> </ul>
 

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