Don't use __slots__ (was Re: performance of dictionary lookup vs. object attributes)

Jacob Hallen jacob at cd.chalmers.se
Sun Aug 27 19:59:56 EDT 2006


In article <1156625365.346825.18630 at 74g2000cwt.googlegroups.com>,
Patrick Maupin <pmaupin at gmail.com> wrote:
>I didn't actually sense any dander on your part, so it was probably a
>bit unfortunate that I chose to respond to that particular message.  I
>do (rightly or wrongly) sense some dander on Aahz's part, and this was
>the second or third thread where I saw him respond in a similar terse
>fashion, but I wasn't really motivated to respond until I saw your
>response to his response to, well, you know...
>
>And I know that the BDFL thinks it's a mistake, and he's probably
>right.  In my previous post, I attempted to rationalize why this is
>true.  For a start, Guido is probably continually hounded by people
>asking stupid __slots__ questions.
>
>Nonetheless, this is one of the few topics on CLP where innocent,
>rational questions are more often than not answered with "because I
>said so", so I was trying to inject a bit of nuance into the
>discussion.  (For most similar technical questions, someone will post a
>pointer to a  post or document which explains in clear technical detail
>WHY things are bad, but for this issue there seems to be no comparable
>source -- just that rant of the BDFL, which certainly carries a lot of
>weight in terms of best practices, but which I find woefully inadequate
>in terms of explanations which are dumbed-down enough for me to
>follow.)
>
>Also, as I noted, I _do_ use them on occasion, so if there really _are_
>potential pitfalls there, I would like to understand exactly what they
>are, so my ears perk up whenever I notice a __slots__ discussion, but
>so far I have been repeatedly disappointed, in that I always see
>someone saying "don't do that" but I have never seen a cogent technical
>argument about how my scripts which __slots__ are going to suddenly,
>irretrievably break one day.

The proper use of__slots__is to save space in objects. Instead of having
a dynamic dict that allows adding attributes to objects at anytime,
there is a static structure which does not allow additions after creation.
This saves the overheadof one dict for every object that uses slots.
While this is sometimes a useful optimisation, it would be completely
unnecessary if the Python interpreter was dynamic enough so that it would
only require the dict when there actually were additions to the object.

Unfortunately there is a side effect to slots. They change the behaviour of
the objects that have slots in a way that can be abused by control freaks
and static typing weenies. This is bad, because the contol freaks should
be abusing the metaclasses and the static typing weenies should be abusing
decorators, since in Python,there should be only one obvious way of doing something.

Making CPython smart enough to handle saving space without __slots__ is a a major
undertaking, which is probably why it is not on the list of changes for P3k (yet).

Jacob Hallén

-- 



More information about the Python-list mailing list