[Python-Dev] defaultdict proposal round three (original) (raw)

Guido van Rossum guido at python.org
Mon Feb 20 14:41:43 CET 2006


I'm withdrawing the last proposal. I'm not convinced by the argument that contains should always return True (perhaps it should also insert the value?), nor by the complaint that a holy invariant would be violated (so what?).

But the amount of discussion and the number of different viewpoints present makes it clear that the feature as I last proposed would be forever divisive.

I see two alternatives. These will cause a different kind of philosophical discussion; so be it. I'll describe them relative to the last proposal; for those who wisely skipped the last thread, here's a link to the proposal: http://mail.python.org/pipermail/python-dev/2006-February/061261.html.

Alternative A: add a new method to the dict type with the semantics of getattr from the last proposal, using default_factory if not None (except on_missing is inlined). This avoids the discussion about broken invariants, but one could argue that it adds to an already overly broad API.

Alternative B: provide a dict subclass that implements the getattr semantics from the last proposal. It could be an unrelated type for all I care, but I do care about implementation inheritance since it should perform just as well as an unmodified dict object, and that's hard to do without sharing implementation (copying would be worse).

Parting shots:

was questioned, and I'm not sure that

d.something(key, list).append(value)

is any more readable. IOW I like (and I believe few have questioned) associating the default factory with the dict object instead of with the call site.

Let the third round of the games begin!

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list