Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I had to derive the reasoning from first principles, I might think about it in terms of the <a href="http://docs.python.org/reference/datamodel.html#slots" rel="nofollow"><code>__slots__</code></a> mechanism. Some kinds of classes only need a tiny number of attributes, and the set of attributes is always fixed for every instance, and so the flexibility of a "dict" like user attributes capability would be wasteful of cpu (looking up dynamically named attributes) and space (the overhead of an empty dict, small but non-zero), which can really add up to a lot of overhead if there are a large number of instances.</p> <p>of course this is exactly the problem the <code>__slots__</code> mechanism solves. space is allocated on the object for the attributes defined in slots. if there's a <code>__dict__</code> attribute, non-slot attributes can be found there, and if there's no <code>__dict__</code> attribute, you get neither the dynamic attributes nor the cost of the unneeded dict object (which is an additional heap object other than the instance itself).</p> <p>As for why <code>object</code> itself does not have a slot for <code>__dict__</code>, well, even if a subclass indicated that they didn't <em>need</em> the <code>__dict__</code> slot, if they inherit from a class that <em>does</em>, then the instance must still have a <code>__dict__</code>; if <code>object</code> had a <code>__dict__</code> then absolutely every class would also pay the price of having a <code>__dict__</code>, even if they didn't need it.</p> <p>Instead; subclasses get <code>__dict__</code> (and <code>__weakref__</code>, for analogous reasons) unless they themselves define a <code>__slots__</code> class attribute.</p> <p>And why would you need a plain-ol instance of <code>object</code> at all? Do you just need a bag of attributes? well then use a <code>dict</code>. If you actually need an object of some kind with behaviors and attributes, then you are making a subclass anyways.</p> <p>About the only reason I can ever imagine using <code>object()</code> instead of a subclass of <code>object</code>, is when the only feature of the instance that is of interest is its identity. This would come up when a function takes arguments, and for which <code>None</code> or some other obvious sentinel value should be understood as distinct from the default:</p> <pre><code>BAR_DEFAULT = object() def foo(bar=BAR_DEFAULT): if bar is BAR_DEFAULT: #... </code></pre> <p>In which case you neither need nor particularly want attributes on the instance of <code>object</code>, </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.
    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