[Python-Dev] Proposal: defaultdict (original) (raw)
Guido van Rossum guido at python.org
Fri Feb 17 23:15:39 CET 2006
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 2/17/06, Ian Bicking <ianb at colorstudy.com> wrote:
Guido van Rossum wrote: > d = {} > d.defaultfactory = set > ... > d[key].add(value)
Another option would be: d = {} d.defaultfactory = set d.getdefault(key).add(value) Unlike .setdefault, this would use a factory associated with the dictionary, and no default value would get passed in. Unlike the proposal, this would not override getitem (not overriding getitem is really the only difference with the proposal). It would be clear reading the code that you were not implicitly asserting they "key in d" was true. "getdefault" isn't the best name, but another name isn't jumping out at me at the moment. Of course, it is not a Pythonic argument to say that an existing method should be overridden, or functionality made nameless simply because we can't think of a name (looking to anonymous functions of course ;)
I'm torn. While trying to implement this I came across some ugliness in PyDict_GetItem() -- it would make sense if this also called on_missing(), but it must return a value without incrementing its refcount, and isn't supposed to raise exceptions -- so what to do if on_missing() returns a value that's not inserted in the dict?
If the getattr()-like operation that supplies and inserts a dynamic default was a separate method, we wouldn't have this problem.
OTOH most reviewers here seem to appreciate on_missing() as a way to do various other ways of alterning a dict's getitem() behavior behind a caller's back -- perhaps it could even be (ab)used to implement case-insensitive lookup.
I'm not going to do a point-by-point to your longer post (I don't have the time); let's (again) agree to disagree and I'll sleep on it.
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Proposal: defaultdict
- Next message: [Python-Dev] Proposal: defaultdict
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]