Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Notice that an <code>object</code> instance has no <code>__dict__</code> attribute:</p> <pre><code>&gt;&gt;&gt; dir(object()) ['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__'] </code></pre> <p>An example to illustrate this behavior in a derived class:</p> <pre><code>&gt;&gt;&gt; class Foo(object): ... __slots__ = {} ... &gt;&gt;&gt; f = Foo() &gt;&gt;&gt; f.bar = 42 Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; AttributeError: 'Foo' object has no attribute 'bar' </code></pre> <p>Quoting from the docs on <a href="https://docs.python.org/2/reference/datamodel.html#slots" rel="nofollow noreferrer"><code>slots</code></a>:</p> <blockquote> <p>[...] The <code>__slots__</code> declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because <code>__dict__</code> is not created for each instance.</p> </blockquote> <p>EDIT: To answer ThomasH from the comments, OP's test class is an "old-style" class. Try:</p> <pre><code>&gt;&gt;&gt; class test: pass ... &gt;&gt;&gt; getattr(test(), '__dict__') {} &gt;&gt;&gt; getattr(object(), '__dict__') Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; AttributeError: 'object' object has no attribute '__dict__' </code></pre> <p>and you'll notice there is a <code>__dict__</code> instance. The object class may not have a <code>__slots__</code> defined, but the result is the same: lack of a <code>__dict__</code>, which is what prevents dynamic assignment of an attribute. I've reorganized my answer to make this clearer (move the second paragraph to the top).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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