[Python-Dev] Proposal: defaultdict (original) (raw)
Raymond Hettinger python at rcn.com
Fri Feb 17 05:27:17 CET 2006
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
I would like to add something like this to the collections module, but a PEP is probably needed to deal with issues like:
- implications of a getitem succeeding while get(value, x) returns x (possibly different from the overall default)
- implications of a getitem succeeding while contains would fail
- whether to add this to the collections module (I would say yes)
- whether to allow default functions as well as default values (so you could instantiate a new default list)
- comparing all the existing recipes and third-party modules that have already done this
- evaluating its fitness for common use cases (i.e. bags and dict of lists).
- lay out a few examples:
bag like behavior
dd = collections.default_dict() dd.default(0) for elem in collection: dd[elem] += 1
setdefault-like behavior
dd = collections.default_dict() dd.default(list) # instantiate a new list for empty cells for page_number, page in enumerate(book): for word in page.split(): dd[word].append(word)
Raymond
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]