| Store | Cart

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

From: Patrick Maupin <pmau...@gmail.com>
28 Aug 2006 17:30:16 -0700
Dieter Maurer wrote:
> "Patrick Maupin" <pmaupin at gmail.com> writes on 26 Aug 2006 12:51:44 -0700:> > ...> > The only> > problem I personally know of is that the __slots__ aren't inherited,>> "__slots__" *ARE* inherited, although the rules may be a bit> complex.

Yes, I didn't write that correctly.  What's not inherited is the lack
of a dictionary :)

>>> class foo(object):
...   __slots__ = 'a b c'.split()
...
>>> class bar(foo):
...     pass
...
>>> set(dir(foo())) - set(dir(bar()))
set([])
>>> set(dir(bar())) - set(dir(foo()))
set(['__dict__', '__weakref__'])

And you are correct that the rules are a bit complicated, but for my
purposes (memory usage reduction on non-subclassed classes) __slots__
seem to work fine.

Thanks,
Pat

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