[Python-Dev] Proposal: defaultdict
Adam Olsen
rhamph at gmail.com
Fri Feb 17 15:13:51 CET 2006
More information about the Python-Dev mailing list
Fri Feb 17 15:13:51 CET 2006
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2/16/06, Guido van Rossum <guido at python.org> wrote: > A bunch of Googlers were discussing the best way of doing the > following (a common idiom when maintaining a dict of lists of values > relating to a key, sometimes called a multimap): > > if key not in d: d[key] = [] > d[key].append(value) > > An alternative way to spell this uses setdefault(), but it's not very readable: > > d.setdefault(key, []).append(value) I'd like to see it done passing a factory function (and with a better name): d.getorset(key, list).append(value) The name is slightly odd but it is effective. Plus it avoids creating a new class when a slight tweak to an existing one will do. > Over lunch with Alex Martelli, he proposed that a subclass of dict > with this behavior (but implemented in C) would be a good addition to > the language. It looks like it wouldn't be hard to implement. It could > be a builtin named defaultdict. The first, required, argument to the > constructor should be the default value. Remaining arguments (even > keyword args) are passed unchanged to the dict constructor. -1 (atleast until you can explain why that's better than .getorset()) -- Adam Olsen, aka Rhamphoryncus
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list