Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To support arbitrary attribute assignment, an object needs a <code>__dict__</code>: a dict associated with the object, where arbitrary attributes can be stored. Otherwise, there's nowhere to <em>put</em> new attributes.</p> <p>An instance of <code>object</code> does <strong>not</strong> carry around a <code>__dict__</code> -- if it did, before the horrible circular dependence problem (since <code>dict</code>, like most everything else, inherits from <code>object</code>;-), this would saddle <em>every</em> object in Python with a dict, which would mean an overhead of <em>many</em> bytes per object that currently doesn't have or need a dict (essentially, all objects that don't have arbitrarily assignable attributes don't have or need a dict).</p> <p>For example, using the excellent <code>pympler</code> project (you can get it via svn from <a href="http://code.google.com/p/pympler/source/checkout" rel="noreferrer">here</a>), we can do some measurements...:</p> <pre><code>&gt;&gt;&gt; from pympler import asizeof &gt;&gt;&gt; asizeof.asizeof({}) 144 &gt;&gt;&gt; asizeof.asizeof(23) 16 </code></pre> <p>You wouldn't want every <code>int</code> to take up 144 bytes instead of just 16, right?-)</p> <p>Now, when you make a class (inheriting from whatever), things change...:</p> <pre><code>&gt;&gt;&gt; class dint(int): pass ... &gt;&gt;&gt; asizeof.asizeof(dint(23)) 184 </code></pre> <p>...the <code>__dict__</code> <em>is</em> now added (plus, a little more overhead) -- so a <code>dint</code> instance can have arbitrary attributes, but you pay quite a space cost for that flexibility.</p> <p>So what if you wanted <code>int</code>s with just <em>one</em> extra attribute <code>foobar</code>...? It's a rare need, but Python does offer a special mechanism for the purpose...</p> <pre><code>&gt;&gt;&gt; class fint(int): ... __slots__ = 'foobar', ... def __init__(self, x): self.foobar=x+100 ... &gt;&gt;&gt; asizeof.asizeof(fint(23)) 80 </code></pre> <p>...not <em>quite</em> as tiny as an <code>int</code>, mind you! (or even the two <code>int</code>s, one the <code>self</code> and one the <code>self.foobar</code> -- the second one can be reassigned), but surely much better than a <code>dint</code>.</p> <p>When the class has the <code>__slots__</code> special attribute (a sequence of strings), then the <code>class</code> statement (more precisely, the default metaclass, <code>type</code>) does <strong>not</strong> equip every instance of that class with a <code>__dict__</code> (and therefore the ability to have arbitrary attributes), just a finite, rigid set of "slots" (basically places which can each hold one reference to some object) with the given names.</p> <p>In exchange for the lost flexibility, you gain a lot of bytes per instance (probably meaningful only if you have zillions of instances gallivanting around, but, there <em>are</em> use cases for that).</p>
    singulars
    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.
    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