![](https://secure.gravatar.com/avatar/b61315ad4ac9a887387d4ee835bd80c0.jpg?s=120&d=mm&r=g)
Feb. 17, 2006
12:04 p.m.
Guido van Rossum wrote:
So here's a new proposal.
Let's add a generic missing-key handling method to the dict class, as well as a default_factory slot initialized to None. The implementation is like this (but in C):
def on_missing(self, key): if self.default_factory is not None: value = self.default_factory() self[key] = value return value raise KeyError(key)
When __getitem__() (and *only* __getitem__()) finds that the requested key is not present in the dict, it calls self.on_missing(key) and returns whatever it returns -- or raises whatever it raises. __getitem__() doesn't need to raise KeyError any more, that's done by on_missing().
Will this also work when PyDict_GetItem() does not find the key? Thomas