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

Carl Banks pavlovevidence at gmail.com
Sat Aug 26 17:16:33 EDT 2006


Patrick Maupin wrote:
> The only assertion that was made explicitly enough to be testable came
> about in a followup to Aahz's original post, only AFTER someone asked
> what the side-effects associated with __slots__ were.  Aahz responded:
>
> > The main one is that inheritance becomes difficult to nearly-impossible.
>
> But this statement is either wrong or incomplete.
[snip]

It's definitely an overstatement.

I myself have written a library (Dice3DS, q.v., which is a pure-Python
3DS library), which used both __slots__ and inheritance extensively,
with no problems.  I wouldn't even say it was difficult, let alone
nearly impossible, but it definitely required care.

Dice3DS was my learning experience with metaclasses (if you want to
talk about difficult :).  I wrote it soon after new-style classes made
their debut, and didn't realize what the intended purpose of __slots__
was.  I definitely wouldn't use __slots__ if I were it writing it
today; instead, I'd use a homemade attribute checker that was free from
__slots__'s weird nuances.

In fairness to myself, some 3DS files have thousands or tens of
thousands of chunks, so using __slots__ probably did save quite a bit
of byteage, but that consideration had nothing to do with my decision
to use __slots__.


Carl Banks




More information about the Python-list mailing list