Q on document Unifying types and classes in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rim

    Q on document Unifying types and classes in Python

    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
  • Martin v. Löwis

    #2
    Re: Q on document Unifying types and classes in Python

    rimbalaya@yahoo .com (Rim) writes:
    [color=blue]
    > "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.[/color]

    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.
    [color=blue]
    > 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.[/color]

    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

    Comment

    Working...