[Python-Dev] Proposal: defaultdict
Alex Martelli
aleaxit at gmail.com
Fri Feb 17 20:34:46 CET 2006
On 2/16/06, Guido van Rossum <guido at python.org> wrote:
> A bunch of Googlers were discussing the best way of doing the
...
Wow, what a great discussion! As you'll recall, I had also mentioned
the "callable factory" as a live possibility, and there seems to be a
strong sentiment in favor of that; not really a "weakness case" for
HOFs, as you feared it might be during the lunchtime discussion.
Out of all I've read here, I like the idea of having a
collections.autodict (a much nicer name than defaultdict, a better
collocation for 2.5 than the builtins). One point I think nobody has
made is that whenever reasonably possible the setting of a callback
(the callable factory here) should include *a and **k to use when
calling back. So, for example:
ad = collections.autodict(copy.copy, whatever)
would easily cover the use case of Google's DefaultDict (yes, partial
would also cover this use case, but having *a and **k is usefully more
general). If you're convinced copy.copy is an overwhelmingly popular
use case (I'm not, personally), then this specific idiom might also be
summarized in a classmethod, a la
ad = collections.autodict.by_copy(whatever)
This way, all autodicts would start out empty (and be filled by update
if needed). An alternative would be to have autodict's ctor have the
same signature as dict's, with a separate .set_initial method to pass
the factory (and *a, **k) -- this way an autodict might start out
populated, but would always start with some default factory, such as
lambda:None I guess. I think the first alternative (autodict always
starts empty, but with a specifically chosen factory [including *a,
**k]) is more useful.
Alex
More information about the Python-Dev
mailing list