Q on document Unifying types and classes in Python

Discussion in 'Python' started by Rim, Jun 30, 2003.

  1. Rim

    Rim Guest

    Hi,

    I am reading the document http://www.python.org/2.2.1/descrintro.html

    "This is not always what you want; in particular, using a separate
    dictionary to hold a single instance variable doubles the memory used
    by a defaultdict instance compared to using a regular dictionary!"

    I don't understand what is the separate dictionary Guido is talking
    about.

    I also don't understand how one additional entry (the 'default' entry)
    in the dictionary doubles the dictionary size... except when the
    dictionary has only one
    element, then of course, adding another element will cause it double
    in
    number of elements.

    Thanks,
    - Rim
    Rim, Jun 30, 2003
    #1
    1. Advertising

  2. (Rim) writes:

    > "This is not always what you want; in particular, using a separate
    > dictionary to hold a single instance variable doubles the memory used
    > by a defaultdict instance compared to using a regular dictionary!"
    >
    > I don't understand what is the separate dictionary Guido is talking
    > about.


    The defaultdict *is-a* dictionary in itself; it also *has-a*
    dictionary: the dictionary of instance variables (a.__dict__). The
    second one is a separate dictionary, one that doesn't exist for a
    dict() object.

    > I also don't understand how one additional entry (the 'default'
    > entry) in the dictionary doubles the dictionary size... except when
    > the dictionary has only one element, then of course, adding another
    > element will cause it double in number of elements.


    The fixed overhead doubles. Since dictionaries start off with 8 slots,
    both the 'a' dictionary itself, and 'a.__dict__' still consume this
    initial size only, doubling memory consumption. As 'a' proper fills
    up, memory consumption won't be twice as large anymore compared to the
    'proper dictionary' case.

    HTH,
    Martin
    Martin v. =?iso-8859-15?q?L=F6wis?=, Jun 30, 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. Steve Knight
    Replies:
    2
    Views:
    757
    Steve Knight
    Oct 10, 2003
  2. Replies:
    8
    Views:
    1,355
    Victor Bazarov
    Jan 7, 2006
  3. Replies:
    5
    Views:
    343
    James Kanze
    Apr 27, 2007
  4. Eric S. Johansson

    unifying many packages under one name

    Eric S. Johansson, Aug 24, 2007, in forum: Python
    Replies:
    2
    Views:
    288
    James Stroud
    Aug 24, 2007
  5. Joel VanderWerf

    unifying hash and proc syntax

    Joel VanderWerf, Mar 22, 2005, in forum: Ruby
    Replies:
    3
    Views:
    103
    Csaba Henk
    Mar 22, 2005
Loading...

Share This Page