473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 1482
<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
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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,...

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.