__slots__ replacing __dict__ function

Discussion in 'Python' started by anabell@sh163.net, Nov 6, 2003.

  1. Guest

    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.
     
    , Nov 6, 2003
    #1
    1. Advertising

  2. 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
     
    Alex Martelli, Nov 6, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Fountain

    When is a __dict__ not a __dict__?

    Derek Fountain, Apr 21, 2004, in forum: Python
    Replies:
    1
    Views:
    335
    John Roth
    Apr 21, 2004
  2. Jean Brouwers

    __slots__ vs __dict__

    Jean Brouwers, May 12, 2004, in forum: Python
    Replies:
    5
    Views:
    515
    Marcus von Appen
    May 13, 2004
  3. Hornberger, Chris

    RE: __slots__ vs __dict__

    Hornberger, Chris, May 12, 2004, in forum: Python
    Replies:
    1
    Views:
    428
    Jean Brouwers
    May 12, 2004
  4. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    282
    Chris Hohmann
    Apr 11, 2005
  5. Ulrich Eckhardt

    replacing __dict__ with an OrderedDict

    Ulrich Eckhardt, Jan 6, 2012, in forum: Python
    Replies:
    35
    Views:
    1,181
    Ulrich Eckhardt
    Jan 18, 2012
Loading...

Share This Page