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

Patrick Maupin pmaupin at gmail.com
Sat Aug 26 16:49:25 EDT 2006


skip at pobox.com wrote:
> Aahz> Taking a look at __slots__ is fine as long as you don't actually
>     Aahz> use them.
>
>     Gabriel> Why?
>
>     Skip> http://groups.google.com/group/comp.lang.python/browse_thread/thread/451ad25f9c648404/f4ac2dfde32b16fd?lnk=st&q=Python+__slots__+aahz&rnum=2#f4ac2dfde32b16fd
>
>     Patrick> The subject of __slots__ really seems to get some people's
>     Patrick> dander up, to the extent where the heat/light ratio in the
>     Patrick> discussion becomes uncomfortably high.  Right here, we have
>     Patrick> Skip referring to a post by Aahz, where Aahz says that Guido
>     Patrick> says that slots are bad mojo, without anybody ever giving
>     Patrick> concrete examples about why this may be the case.  The only
>     Patrick> assertion that was made explicitly enough to be testable came
>     Patrick> about in a followup to Aahz's original post, only AFTER someone
>     Patrick> asked what the side-effects associated with __slots__ were.
>     Patrick> Aahz responded:
>
> No dander on my part.  I was just pointing out an earlier thread on the
> topic.  Note however that the ultimate source of the anti-slots fervor in
> that thread is Guido himself (may he live long and prosper as BDFL).  If
> Guido thinks it's bad mojo, that's good enough for me.  Also, if he thinks
> it's bad mojo now, my suspicion is that you won't see it in Py3k.
>
> That said,  It's not mentioned on the Python3.0 page of the wiki:
>
>     http://wiki.python.org/moin/Python3.0
>
> or in PEP 3000:
>
>     http://www.python.org/dev/peps/pep-3000/
>
> and I see no discussion about it in the Python 3000 mailing list archives:
>
>     http://mail.python.org/pipermail/python-3000/
>
> though Ian Bicking asks about it here:
>
>     http://wiki.python.org/moin/Python3%2e0Suggestions#head-fc89a0fe3f697418776925f4828ea863031fbbd2
>
> Skip

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.

Regards,
Pat




More information about the Python-list mailing list