468,247 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

So what are __slots__ and when should I use them?

I see some programs declaring the names of class variables in
"__slots__". I've looked this up and the docs say something about old
and new style classes, whatever that means. Can someone give me a
simple, brief explanation of what __slots__ are and when I should use
them? Thanks.

Nov 22 '05 #1
4 1321
<da*********@gmail.com> wrote:
I see some programs declaring the names of class variables in
"__slots__". I've looked this up and the docs say something about old
and new style classes, whatever that means. Can someone give me a
simple, brief explanation of what __slots__ are and when I should use
them? Thanks.


Normally, for flexibility and simplicity, every class instance carries
around a dictionary -- which is great, and very fast, _but_ does take up
a little memory per-instance. __slots__ lets you make a class whose
instances does NOT carry a dictionary, saving tens of bytes, at a price
in simplicity and flexibility. So, it's useful for classes whose
instances are little more than holders for a few small pieces of data,
ideally don't use inheritance (__slots__ and inheritance can mix, but
not trivially so), AND as long as you plan to have HUGE numbers of
instances of such classes "alive" at the same time, so that it matters a
lot to you to save those few bytes per instance. The classes must be
new-type (inherit from object or some other built-in type).

Many people try to use one of __slots__'s unfortunate side effects (the
fact that it removes flexibility from the instances of classes that
define it) for other purposes (presumably because they're coming from
languages where class instances don't HAVE that flexibility, and they're
trying to use Python as if it was a different language, rather than
using Python as Python). However, I heartily recommend that you
consider defining __slots__ only as an optimization (memory saving),
should you ever find yourself in a situation meeting all of the
requirements in the previous paragraph (if ever).
Alex
Nov 22 '05 #2
<da*********@gmail.com> wrote:
I see some programs declaring the names of class variables in
"__slots__". I've looked this up and the docs say something about old
and new style classes, whatever that means. Can someone give me a
simple, brief explanation of what __slots__ are and when I should use
them? Thanks.


Normally, for flexibility and simplicity, every class instance carries
around a dictionary -- which is great, and very fast, _but_ does take up
a little memory per-instance. __slots__ lets you make a class whose
instances does NOT carry a dictionary, saving tens of bytes, at a price
in simplicity and flexibility. So, it's useful for classes whose
instances are little more than holders for a few small pieces of data,
ideally don't use inheritance (__slots__ and inheritance can mix, but
not trivially so), AND as long as you plan to have HUGE numbers of
instances of such classes "alive" at the same time, so that it matters a
lot to you to save those few bytes per instance. The classes must be
new-type (inherit from object or some other built-in type).

Many people try to use one of __slots__'s unfortunate side effects (the
fact that it removes flexibility from the instances of classes that
define it) for other purposes (presumably because they're coming from
languages where class instances don't HAVE that flexibility, and they're
trying to use Python as if it was a different language, rather than
using Python as Python). However, I heartily recommend that you
consider defining __slots__ only as an optimization (memory saving),
should you ever find yourself in a situation meeting all of the
requirements in the previous paragraph (if ever).
Alex
Nov 22 '05 #3
Here is an example of the difference between a class with __slots__ and
__dict__

<http://mail.python.org/pipermail/python-list/2004-May/220513.html>

/Jean Brouwers

Nov 22 '05 #4
Here is an example of the difference between a class with __slots__ and
__dict__

<http://mail.python.org/pipermail/python-list/2004-May/220513.html>

/Jean Brouwers

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by flori | last post: by
3 posts views Thread by Nick Jacobson | last post: by
7 posts views Thread by Porky Pig Jr | last post: by
2 posts views Thread by Ewald R. de Wit | last post: by
reply views Thread by dan.j.weber | last post: by
3 posts views Thread by Schüle Daniel | last post: by
3 posts views Thread by John Machin | last post: by
27 posts views Thread by Licheng Fang | last post: by
19 posts views Thread by jsanshef | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.