| Store | Cart

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

From: Jacob Hallen <jac...@cd.chalmers.se>
Sun, 27 Aug 2006 23:59:56 +0000 (UTC)
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

-- 

Recent Messages in this Thread
bear...@lycos.com Aug 25, 2006 12:15 pm
Aahz Aug 25, 2006 02:34 pm
Gabriel Genellina Aug 25, 2006 03:23 pm
s...@pobox.com Aug 25, 2006 03:33 pm
Patrick Maupin Aug 26, 2006 07:51 pm
s...@pobox.com Aug 26, 2006 08:28 pm
Carl Banks Aug 26, 2006 09:16 pm
Dieter Maurer Aug 28, 2006 07:26 pm
Patrick Maupin Aug 29, 2006 12:30 am
Jarek Zgoda Aug 26, 2006 08:35 pm
Patrick Maupin Aug 26, 2006 08:51 pm
Patrick Maupin Aug 26, 2006 08:49 pm
bear...@lycos.com Aug 25, 2006 05:38 pm
Peter Otten Aug 25, 2006 12:48 pm
Fredrik Lundh Aug 25, 2006 01:03 pm
Jacob Hallen Aug 27, 2006 11:59 pm
Patrick Maupin Aug 28, 2006 12:52 am
David Isaac Aug 28, 2006 03:49 am
Antoon Pardon Aug 29, 2006 07:07 am
Messages in this thread