473,480 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1485
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3063
by: flori | last post by:
i try to greate somthing like this class ca(object): __slots__ = ("a",) class cb(ca): __slots__ = ("a","b") class cc(ca): __slots__ = ("a","c") class cd(cb,cc): __slots__ = ("a","b","c","d") ...
3
1627
by: Nick Jacobson | last post by:
The __slots__ attribute of new-style classes can reduce memory usage when there are millions of instantiations of a class. So would a __slots__ attribute for functions/methods have a similar...
7
1651
by: Porky Pig Jr | last post by:
Hello, I"m still learning Python, but going through the Ch 5 OOP of Nutshell book. There is discussion on __slots__, and my understanding from reading this section is that if I have a class...
2
4960
by: Ewald R. de Wit | last post by:
I'm running into a something unexpected for a new-style class that has both a class attribute and __slots__ defined. If the name of the class attribute also exists in __slots__, Python throws an...
0
232
by: dan.j.weber | last post by:
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...
3
1560
by: Schüle Daniel | last post by:
Hello, consider this code >>> class A(object): .... def __init__(self): .... self.a = 1 .... self.b = 2 .... >>> class B(A):
3
1609
by: John Machin | last post by:
I have stumbled across some class definitions which include all/most method names in a __slots__ "declaration". A cut-down and disguised example appears at the end of this posting. Never mind...
27
1694
by: Licheng Fang | last post by:
Python is supposed to be readable, but after programming in Python for a while I find my Python programs can be more obfuscated than their C/C ++ counterparts sometimes. Part of the reason is that...
19
4607
by: jsanshef | last post by:
Hi, after a couple of days of script debugging, I kind of found that some assumptions I was doing about the memory complexity of my classes are not true. I decided to do a simple script to...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4472
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.