473,503 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

__slots__ replacing __dict__ function

I have a code like this:

sqlString = 'INSERT INTO ' + self.TableName + ' VALUES (' + self.TableFields + ')'
self.cursor.execute(sqlString, self.__dict__)

This works correctly. However, I'm applying __slots__ in my script. And doing so would need the above statement modified. How will __slots__ perform the same task defined above?

Is there a container that holds the values of attributes contained in __slots__? In __dict__, you have (attribute: value) pair.
Jul 18 '05 #1
1 1979
an*****@sh163.net wrote:
I have a code like this:

sqlString = 'INSERT INTO ' + self.TableName + ' VALUES (' +
self.TableFields + ')' self.cursor.execute(sqlString,
self.__dict__)

This works correctly. However, I'm applying __slots__ in my script. And
doing so would need the above statement modified. How will __slots__
perform the same task defined above?

Is there a container that holds the values of attributes contained in
__slots__? In __dict__, you have (attribute: value) pair.


Each instance you have simultaneously alive that is using __slots__
and therefore saving the per-instance __dict__ will save you a few
tens of bytes -- say, optimistically, 64 bytes if you have quite a
few attributes per instance.

Will you have as many as, say, 100,000 instances simultaneously
alive, and, if so, will saving about 6 MB of memory be crucially
important to your application's performance?

For a typical class that's unlikely to exist in more than a few
thousands of instances alive at a time, saving a few tens of KB
of memory is an absolutely derisory benefit and will emphatically
_NOT_ repay the extra programming effort to use __slots__.

Have you performed this back-of-the-envelope estimate? Is the
use of __slots__ truly justified in your case?

Don't use __slots__ just to try and get back closer to the
behavior of some other language you're used to -- or else you'll
get back all the _hassles_ typical of those other languages,
fully including more laborious reflection and introspection.
Alex

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2184
by: simon place | last post by:
Removing __slots__ ~~~~~~~~~~~~~~~~~~~ To do this nicely requires the renaming of __dict__ to, say, __attribs__ , ( since dict is unnecessarily unspecific, this seem like a small improvement...
5
2911
by: Jean Brouwers | last post by:
Classes using __slots__ seem to be quite a bit smaller and faster to instantiate than regular Python classes using __dict__. Below are the results for the __slots__ and __dict__ version of a...
3
1629
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...
1
1551
by: Alex | last post by:
I would greatly appreciate an advice on the following matter that was very much discussed circa 2002 but in all this discussion I could not find the final answer with a code example. Neither I can...
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):
15
531
by: David Isaac | last post by:
1. "Without a __dict__ variable, instances cannot be assigned new variables not listed in the __slots__ definition." So this seemed an interesting restriction to impose in some instances, but...
10
1654
by: Tom Plunket | last post by:
I've got a bunch of code that runs under a bunch of unit tests. It'd be really handy if when testing I could supply replacement functionality to verify that the right things get called without...
27
1695
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...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
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
7458
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
5578
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
5013
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
4672
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.