Q on document Unifying types and classes in Python

Martin v. Löwis martin at v.loewis.de
Sun Jun 29 23:49:50 EDT 2003


rimbalaya at yahoo.com (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




More information about the Python-list mailing list