468,257 Members | 1,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

__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 1787
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by simon place | last post: by
5 posts views Thread by Jean Brouwers | last post: by
3 posts views Thread by Nick Jacobson | last post: by
7 posts views Thread by Porky Pig Jr | last post: by
1 post views Thread by Alex | last post: by
3 posts views Thread by Schüle Daniel | last post: by
15 posts views Thread by David Isaac | last post: by
10 posts views Thread by Tom Plunket | last post: by
27 posts views Thread by Licheng Fang | 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.