[Python-Dev] Proposal: defaultdict

Jim Jewett jimjjewett at gmail.com
Mon Feb 20 20:02:02 CET 2006

Adam Olsen asked:
> ... d.getorset(key, func) would work in your use
> cases?

It is an improvement over setdefault, because it
doesn't always evaluate the expensive func.  (But
why should every call have to pass in the function,
when it is a property of the dictionary?)

It doesn't actually *solve* the problem because it
doesn't compose well.  This makes it hard to use for

(Use case from plucker web reader, where the
config is arguably overdesigned, but ... the version
here is simplified)

There is a system-wide default config.
Users have config files.
A config file can be specified for this program run.

In each of these, settings can be either general
settings or channel-specific.  The end result is that
the value should be pulled from the first of about half
a dozen dictionaries to have an answer.  Because
most settings are never used in most channels, and
several channels are typically run at once, it feels
wrong to pre-build the whole "anything they
might ask" settings dictionary for each of them.  On
the other hand, I certainly don't want to write



even once, let alone every time I get a config value.

In other words, the program would work correctly
if I passed in a normal but huge dictionary; I want
to avoid that for reasons of efficiency.  This isn't the
only use for a mapping, but it is the only one I've
seen where KeyError is "expected" by the
program's normal flow.


More information about the Python-Dev mailing list