Q on document Unifying types and classes in Python

R

Rim

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
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

"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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top