[Python-Dev] Proposal: defaultdict
rhamph at gmail.com
Sat Feb 18 02:29:34 CET 2006
On 2/17/06, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> Adam Olsen wrote:
> > You could pass a float in as well. But if the function is documented
> > as taking a dict, and the programmer expects a dict.. that now has to
> > be changed to "dict without a default". Or they have to code
> > defensively since d[key] may or may not raise KeyError, so they must
> > avoid depending on it either way.
> Can you give an example of real, existing code that will break
> if a such a dict is passed?
I only got halfway through the "grep KeyError" results, but..
Demo/tkinter/guido/AttrDialog.py:121 # Subclasses override self.classes
Lib/weakref.py:56 # Currently uses UserDict but I assume it will
switch to dict eventually
And the pièce de résistance..
It has this:
info = rcdict[s]
sys.stderr.write("No refcount data for %s\n" % s)
rcdict is loaded from refcounts.load(). refcounts.load() calls
refcounts.loadfile(), which has this (inside a loop):
entry = d[function]
entry = d[function] = Entry(function)
A prime candidate for a default.
Perhaps the KeyError shouldn't ever get triggered in this case, I'm
not sure. I think that's besides the point though. The programmer
clearly expected it would.
Adam Olsen, aka Rhamphoryncus
More information about the Python-Dev