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

Matt Gilson matt at getpattern.com
Wed Jun 29 15:29:13 EDT 2016


On Wed, Jun 29, 2016 at 12:09 PM, Guido van Rossum <guido at python.org> wrote:

> So providing the comprehensive base class is up to the user, not up to
> the stdlib. Is that such a big deal?
>

Nope.  I honestly don't think that it is a big deal.  Mostly I just found
the behavior surprising for the reasons mentioned earlier (`__missing__` is
explicitly supported for dict subclasses but also explicitly not supported
on the superclass).

FWIW, "The argument against adding `dict.__missing__()` is that it would
just a shortcut to raise KeyError with certain parameters." also doesn't
seem like that big of a deal to me :-).  I'm not sure if there would
be noticeable performance issues or if the implementation would be
particularly difficult (both reasons which would be certainly compelling
enough to reject this idea).  But if the rational really is that it would
be a hook method whose implementation is almost trivial -- That seems like
the best kind of hook method to me :-).

Of course, maybe I'm shooting my proposal in the foot here -- If it's not
really a big deal on either side of the argument, maybe keeping the
status-quo is the safest recourse...



>
> 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)
> _______________________________________________
> 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/
>



-- 

[image: pattern-sig.png]

Matt Gilson // SOFTWARE ENGINEER

E: matt at getpattern.com // P: 603.892.7736

We’re looking for beta testers.  Go here
<https://www.getpattern.com/meetpattern> to sign up!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160629/d57a1695/attachment.html>


More information about the Python-ideas mailing list