[Python-ideas] should `dict` supply a default `__missing__` implementation?

Ethan Furman ethan at stoneleaf.us
Wed Jun 29 15:07:52 EDT 2016


On 06/29/2016 11:56 AM, Guido van Rossum wrote:

> The argument against adding `dict.__missing__()` is that it would just
> a shortcut to raise KeyError with certain parameters.
>
> The purpose of `__missing__` is to give you a hook to override how the
> base class handles missing keys in `__getitem__`. In order to define
> this hook you must necessarily subclass dict.
>
> IMO the idea that there's always a superclass you can call is silly --
> you should distinguish between cases where you *override* a method vs.
> cases where you *define* it. In this case you are required to *define*
> `__missing__`.

My understanding is a comprehensive base class is necessary when 
multiple inheritance is a concern, since the methods can't know if they 
are being called before or after other methods and so, to be good 
citizens, should call super() -- which of course will fail when the base 
class is searched and the method doesn't exist.

> But I may be missing something...

I can see two possible use-cases for multiple __missing__ methods:

- the default provided may be different based on the key name
- different kinds of tracking/registering of key names is happening

--
~Ethan~


More information about the Python-ideas mailing list