<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2802" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>I'm concerned that the on_missing() part of the 
proposal is gratuitous.&nbsp; The main use cases for defaultdict have a simple 
factory that supplies a zero, empty list, or empty set.&nbsp; The on_missing() 
hook is only there&nbsp;to support the rarer case of needing&nbsp;a key to 
compute a default value.&nbsp; The hook is not needed for the main use 
cases.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>As it stands, we're adding a method to regular 
dicts that cannot be usefully called directly.&nbsp; Essentially, it is a 
framework method meant to be overridden in a subclass.&nbsp; So, it only makes 
sense in the context of subclassing.&nbsp; In the meantime, we've added an 
oddball method to the main dict API, arguably the most important object API in 
Python.&nbsp; </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>To use the hook, you write something like 
this:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; class D(dict):</FONT></DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def 
on_missing(self, key):</FONT></DIV>
<DIV><FONT face=Arial 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return somefunc(key)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>However, we can already do something like that 
without the hook:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; class 
D(dict):<BR>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; def __getitem__(self, 
key):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
try:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return dict.__getitem__(self, 
key)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; except 
KeyError:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp; self[key] = value = 
somefunc(key)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;return value</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>The latter form is already possible, doesn't 
require modifying a basic API, and is arguably clearer about when it is called 
and what it does (the former doesn't explicitly show that the returned value 
gets saved in the dictionary).</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Since we can already do the latter form, 
we&nbsp;can get some insight into whether the need has ever actually arisen in 
real code.&nbsp; I scanned the usual sources (my own code, the standard library, 
and my most commonly used third-party libraries) and found no instances of code 
like that.&nbsp;&nbsp; The closest approximation was safe_substitute() in 
string.Template where missing keys returned themselves as a default value.&nbsp; 
Other than that, I conclude that there isn't sufficient need to warrant adding a 
funky method to the API for regular dicts.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I wondered why the&nbsp;s</FONT><FONT face=Arial 
size=2>afe_substitute() example was unique.&nbsp; I think the answer is that we 
normally handle default computations through simple in-line code ("if k in d: 
do1() else do2()" or a try/except pair).&nbsp; Overriding on_missing() then is 
really only useful when you need to create a type that can be passed to a client 
function that was expecting a regular dictionary.&nbsp; So it does come-up but 
not much.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Aside:&nbsp; Why on_missing() is an oddball among 
dict methods.&nbsp; When teaching dicts to beginner, all the methods are easily 
explainable except this one.&nbsp; You don't call this method directly, you only 
use it when subclassing, you have to override it to do anything useful, it hooks 
KeyError but only&nbsp;when raised by __getitem__ and not other methods, 
etc.&nbsp; I'm concerned that evening having this method in&nbsp;regular 
dict&nbsp;API will create confusion&nbsp;about when to use dict.get(), when to 
use dict.setdefault(), when to catch a KeyError, or when to LBYL.&nbsp; Adding 
this one extra choice makes the choice more difficult.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>My recommendation:&nbsp; Dump the on_missing() 
hook.&nbsp; That leaves the dict API unmolested and&nbsp;allows a more 
straight-forward implementation/explanation of collections.default_dict or 
whatever it ends-up being named.&nbsp; The result is delightfully simple and 
easy to understand/explain.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Raymond</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>