Better dict of dicts

"Martin v. Löwis" martin at v.loewis.de
Fri Apr 20 01:35:25 EDT 2007


> Now suppose there is little overlap between the keys for the outer
> dictionary and the inner dictionaries...but still much overlap between
> the various inner dictionaries.  Then, there is no point in using an
> intern function for the outer dictionary, but still a benefit for the
> inner dictionary.  Thus, something like the following would be appropriate:
> 
> a[k1][local_intern(k2)] = value
> 
> Have I understood this properly?

Correct. Notice that this thing is a time-space-tradeoff resolved in
favor of space - quite uncommon, as people these days always chose to
resolve them in favor of time. Interning a tuple will take some time,
as it is a dictionary lookup (requiring to hash the tuple); then
you will pass the interned tuple again for another dictionary lookup.

So make sure that you don't apply local_intern() twice to the very
same tuple object. E.g. when you also store the tuple in a variable,
don't do

var = (..., ..., ...)
a[foo][local_intern(var)] = foovar
print a[foo][local_intern(var)]

instead do

var = local_intern((..., ..., ...))
a[foo][var] = foovar
print a[foo][var]

Strictly speaking, you can omit the interning on read operation,
as that won't affect the keys stored in the dictionary. However,
performing the lookup with the interned key gives you speed
back, as then dictionary lookup won't need to compare the tuples,
but will find that the search key is identical to the stored
key, so the keys are certainly equal.

Regards,
Martin



More information about the Python-list mailing list