It's ok to __slots__ for what they were intended (was: Don't use __slots__ (was Re: Why custom objects take so much memory?))

Aahz aahz at pythoncraft.com
Thu Dec 20 22:50:31 EST 2007


In article <0829587b-f3a1-4f77-86a1-163e7bcc54d2 at l32g2000hse.googlegroups.com>,
Carl Banks  <pavlovevidence at gmail.com> wrote:
>On Dec 18, 4:49 pm, a... at pythoncraft.com (Aahz) wrote:
>> In article <mailman.2538.1198008758.13605.python-l... at python.org>,
>> Chris Mellon <arka... at gmail.com> wrote:
>>>
>>>You can reduce the size of new-style classes (inherit from object) by
>>>quite a bit if you use __slots__ to eliminate the class dictionary.
>>
>> You can also reduce your functionality quite a bit by using __slots__.
>> Someday I'll have time to write up a proper page about why you shouldn't
>> use __slots__....
>
>Shouting absolute commands without full understanding of the situation
>is not going to help anyone.

Maybe not, but at least it will get people to stop for a bit.

>The OP wanted to minimize memory usage, exactly the intended usage of
>slots.  Without knowing more about the OP's situation, I don't think
>your or I or Chris Mellon can be sure it's not right for the OP's
>situation.

The whole point about warning against __slots__ is that you should never
use them unless you are certain they're the best solution.  Consider what
happens when the OP wants to subclass this __slots__-using class.
Avoiding __slots__ will almost never harm anyone, so I feel completely
comfortable sticking with a blanket warning.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith



More information about the Python-list mailing list