
Guido's on_missing() proposal is pretty good for what it is, but it is not a replacement for set_default(). The use cases for a derivable, definition or instantiation time framework is different than the call-site based decision being made with setdefault(). The difference is that in the former case, the class designer or instantiator gets to decide what the default is, and in the latter (i.e. current) case, the user gets to decide. Going back to first principles, the two biggest problems with today's setdefault() is 1) the default object gets instantiated whether you need it or not, and 2) the idiom is not very readable. To directly address these two problems, I propose a new method called getdefault() with the following signature: def getdefault(self, key, factory) This yields the following idiom: d.getdefault('foo', list).append('bar') Clearly this completely addresses problem #1. The implementation is simple and obvious, and there's no default object instantiated unless the key is missing. I think #2 is addressed nicely too because "getdefault()" shifts the focus on what the method returns rather than the effect of the method on the target dict. Perhaps that's enough to make the chained operation on the returned value feel more natural. "getdefault()" also looks more like "get()" so maybe that helps it be less jarring. This approach also seems to address Raymond's objections because getdefault() isn't "special" the way on_missing() would be. Anyway, I don't think it's an either/or choice with Guido's subclass. Instead I think they are different use cases. I would add getdefault() to the standard dict API, remove (eventually) setdefault(), and add Guido's subclass in a separate module. But I /wouldn't/ clutter the built-in dict's API with on_missing(). -Barry P.S. _missing = object() def getdefault(self, key, factory): value = self.get(key, _missing) if value is _missing: value = self[key] = factory() return value

On Wed, Feb 22, 2006 at 10:29:08PM -0500, Barry Warsaw wrote:
d.getdefault('foo', list).append('bar')
+1. This is a much closer match to my own use of setdefault than Guido's dict subtype. I'm +0 on the subtype, but I prefer the call-time decision on whether to fall back to a default or not. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On Feb 23, 2006, at 4:41 PM, Thomas Wouters wrote:
Cool! As your reward: SF patch #1438113 https://sourceforge.net/tracker/index.php? func=detail&aid=1438113&group_id=5470&atid=305470 -Barry

On Wed, Feb 22, 2006 at 10:29:08PM -0500, Barry Warsaw wrote:
d.getdefault('foo', list).append('bar')
+1. This is a much closer match to my own use of setdefault than Guido's dict subtype. I'm +0 on the subtype, but I prefer the call-time decision on whether to fall back to a default or not. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

On Feb 23, 2006, at 4:41 PM, Thomas Wouters wrote:
Cool! As your reward: SF patch #1438113 https://sourceforge.net/tracker/index.php? func=detail&aid=1438113&group_id=5470&atid=305470 -Barry
participants (2)
-
Barry Warsaw
-
Thomas Wouters