| Store | Cart

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

From: Carl Banks <pavl...@gmail.com>
26 Aug 2006 14:16:33 -0700
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

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