[Python-ideas] should `dict` supply a default `__missing__` implementation?
Guido van Rossum
guido at python.org
Wed Jun 29 15:09:39 EDT 2016
So providing the comprehensive base class is up to the user, not up to
the stdlib. Is that such a big deal?
On Wed, Jun 29, 2016 at 12:07 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> 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~
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list