473,548 Members | 2,598 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.TableField s + ')'
self.cursor.exe cute(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 1982
an*****@sh163.n et wrote:
I have a code like this:

sqlString = 'INSERT INTO ' + self.TableName + ' VALUES (' +
self.TableField s + ')' self.cursor.exe cute(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
2188
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 in itself. ) then using the setting of __attribs__ to a non-mutable type (ie tuple of attribute names) to indicate the behaviour of __slots__,...
5
2919
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 specific class with 16 attributes. Each line in the tables shows the number of instances created so far, the total memory usage in Bytes, the CPU time...
3
1630
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 benefit? i.e. could a function using __slots__ use significantly less memory, and therefore run faster, if called millions of times? If so, it will...
7
1656
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 Rectangle (as defined in some prior sections), and then I provide definition class OptimizedRectangle(Rectangle): __slots__ = 'width', 'heigth' I can...
1
1556
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 find it in Python documentation. I have a series of new classes with child-parent relationship and each has unique __slots__. They don't have...
3
1564
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 I've noticed that this behavior is being called by some a side effect the reliance on which is considered unPythonic. Why? 2. What is a simple...
10
1659
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 those things actually getting called, since frequently those calls take a long time to execute and do things that, well, I don't really want to do. ...
27
1709
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 with heterogeneous lists/tuples at hand, I tend to stuff many things into the list and *assume* a structure of the list or tuple, instead of...
0
7512
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7803
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.