__slots__ replacing __dict__ function

A

anabell

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.
 
A

Alex Martelli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top