Don't use __slots__! (was Re: dicts vs classes)

Aahz aahz at pythoncraft.com
Tue Jul 25 11:10:19 EDT 2006


In article <pan.2006.07.25.10.11.28.303576 at gmx.net>,
Marc 'BlackJack' Rintsch  <bj_666 at gmx.net> wrote:
>In <1153821590.452329.208790 at i3g2000cwc.googlegroups.com>, Guyon Morée
>wrote:
>>
>> I'm using simple classes as a container of named values and I'm
>> instantiating a lot of them in a very short time.
>> 
>> i was wondering if there is any benefit in using dicts instead from a
>> performance/memory usage point of view?
>
>If you really have a memory problem read the documentation about
>`__slots__`.  But I would only consider this if `a lot of` is several 100k
>or millions of objects and the memory consumption really is a problem.

Guido sez:

     __slots__ is a terrible hack with nasty, hard-to-fathom side
     effects that should only be used by programmers at grandmaster and
     wizard levels. Unfortunately it has gained an enormous undeserved
     popularity amongst the novices and apprentices, who should know
     better than to use this magic incantation casually.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan



More information about the Python-list mailing list