__slots_ and inheritance

Aahz aahz at pythoncraft.com
Thu Apr 10 14:31:50 CEST 2003

In article <b7340h0r7m at enews1.newsguy.com>,
Alex Martelli  <aleaxit at yahoo.com> wrote:
>__slots__ is, essentially, an optimization (in terms of memory
>consumption) intended for classes that are likely to be created 
>in very large numbers: you give up the normal flexibility of
>being able to add arbitrary attributes to an instance, and what
>you gain in return is that you save one dictionary per instance
>(dozens and dozens of bytes per instance -- that will, of course,
>not be all that relevant unless the number of instances that are
>around is at the very least in the tens of thousands).

More than that, because of the way modern computer architectures do
caching, using __slots__ tends to speed up instance attribute access.
This also is an optimization, but it's effective with even hundreds of
instances, if you're rapidly switching between instances.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

This is Python.  We don't care much about theory, except where it intersects 
with useful practice.  --Aahz, c.l.py, 2/4/2002

More information about the Python-list mailing list