Python, __slots__, inheritance, and class variables ==> attribute is read-only bug
<p>I have a big tree with hundreds of thousands of nodes, and I'm using <code>__slots__</code> to reduce the memory consumption. I just found a very strange bug and fixed it, but I don't understand the behavior that I saw.</p>

<p>Here's a simplified code sample:</p>

<pre><code>class NodeBase(object):
    __slots__ = ["name"]
    def __init__(self, name):
        self.name = name

class NodeTypeA(NodeBase):
    name = "Brian"
    __slots__ = ["foo"]
</code></pre>

<p>I then execute the following:</p>

<pre><code>&gt;&gt;&gt; node = NodeTypeA("Monty")
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 4, in __init__
AttributeError: 'NodeTypeA' object attribute 'name' is read-only
</code></pre>

<p>There is no error if <code>NodeTypeA.name</code> is not defined (side note: that attribute was there by mistake, and had no reason for being there). There is also no error if <code>NodeTypeA.__slots__</code> is never defined, and it therefore has a <code>__dict__</code>. </p>

<p>The thing I don't understand is: why does the existence of a class variable in a superclass interfere with setting an instance variable in a slot in the child class? </p>

<p>Can anybody explain why this combination results in the <code>object attribute is read-only</code> error? I know my example is contrived, and is unlikely to be intentional in a real program, but that doesn't make this behavior any less strange.</p>

<p>Thanks,<br>
Jonathan</p>
