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

Matt Gilson matt at getpattern.com
Wed Jul 6 01:11:46 EDT 2016


This discussion has my brain all twisted up...  I'm going to try to work my
way through it to make some sense of what we're saying here.

   1. It is pretty easy to provide a subclass with a base `__missing__`
   implementation, so why should python be responsible for providing that
   behavior?
   2. We don't want to encourage subclassing `dict`, so let's not make
   working with `__missing__` slightly nicer (see 1.)
   3. Would adding `__missing__` to `dict` actually make working with it
   slightly nicer anyway?
   4. Other places in the standard library for creating mappings easily
   (`UserDict` and `collections.Mapping`) do not support `__missing__``, but
   it is easy to build that functionality yourself (see 1. and example code
   below)

Let me know if I left anything out or misrepresented any of the comments
... It definitely wasn't my intent.
As I see it, there are three paths forward.


   - Do nothing.  The status quo probably only surprises one or two users a
   year, so lets not rock the boat.  Additionally, we can't add _everything_
   everywhere.  Things just become a mess if you say "Yes" to every idea and
   you have to draw the line somewhere.
   - Add `__missing__` to `dict`.  It might be a simple pass-through method
   for raising `KeyError`, but hey, maybe it'll simplify a code-path or two.
   <https://hg.python.org/cpython/file/tip/Objects/dictobject.c#l1723>
   - Add `__missing__` support to `collections.abc.Mapping`
   <https://hg.python.org/cpython/file/tip/Lib/_collections_abc.py#l595> (or
   I guess UserDict, but I'd be just as happy if that one went away completely
   :-).  I'm sure I haven't thought through all the pitfalls, but (additional
   documentation aside) it seems like it could be as easy as:



class Mapping(Sized, Iterable, Container):
    def __missing__(self, key):
        raise KeyError

    @abc.abstractmethod
    def __getitem__(self, key):
        self.__missing__(key)

    ...


On Tue, Jul 5, 2016 at 3:19 PM, Guido van Rossum <guido at python.org> wrote:

> I think this is a question for Raymond Hettinger.
>
> On Tue, Jul 5, 2016 at 3:13 PM, Neil Girdhar <mistersheik at gmail.com>
> wrote:
> > Is it deprecated?
> >
> > I've seen this question a lot on stackoverflow:
> >
> http://stackoverflow.com/questions/7148419/subclass-dict-userdict-dict-or-abc
> >
> http://stackoverflow.com/questions/2390827/how-to-properly-subclass-dict-and-override-getitem-setitem
> >
> http://stackoverflow.com/questions/10901048/i-want-to-subclass-dict-and-set-default-values
> > I still have no idea what the right answer is.
> >
> > On Tue, Jul 5, 2016 at 6:11 PM Guido van Rossum <guido at python.org>
> wrote:
> >>
> >> Because you shouldn't be using UserDict.
> >>
> >> On Tue, Jul 5, 2016 at 3:07 PM, Neil Girdhar <mistersheik at gmail.com>
> >> wrote:
> >> > Okay, that makes sense, but why isn't __missing__ in UserDict?
> >> >
> >> > On Tue, Jul 5, 2016 at 6:04 PM Guido van Rossum <guido at python.org>
> >> > wrote:
> >> >>
> >> >> What kind of question is that? If you subclass MutableMapping the
> >> >> whole feature doesn't exist (since you're not subclassing dict). It
> >> >> *only* exists for subclasses of dict.
> >> >>
> >> >> On Tue, Jul 5, 2016 at 12:18 PM, Neil Girdhar <mistersheik at gmail.com
> >
> >> >> wrote:
> >> >> > But neither UserDict nor MutableMapping defines __missing__ ?  What
> >> >> > is a
> >> >> > subclasser supposed to do?
> >> >> >
> >> >> > On Wednesday, June 29, 2016 at 4:59:03 PM UTC-4, Guido van Rossum
> >> >> > wrote:
> >> >> >>
> >> >> >> UserDict is superseded by MutableMapping.
> >> >> >>
> >> >> >> I don't think we should make dict a kitchen sink class. I also
> don't
> >> >> >> think we should particularly encourage subclassing it. So -1 on
> >> >> >> adding
> >> >> >> dict.__missing__.
> >> >> >>
> >> >> >> On Wed, Jun 29, 2016 at 12:30 PM, Ethan Furman <
> et... at stoneleaf.us>
> >> >> >> wrote:
> >> >> >> > On 06/29/2016 12:09 PM, Guido van Rossum wrote:
> >> >> >> >
> >> >> >> >> So providing the comprehensive base class is up to the user,
> not
> >> >> >> >> up
> >> >> >> >> to
> >> >> >> >> the stdlib. Is that such a big deal?
> >> >> >> >
> >> >> >> >
> >> >> >> > No, it's not.  But it makes for a better user experience if the
> >> >> >> > base
> >> >> >> > class
> >> >> >> > has the __missing__ method that raises a KeyError already.
> >> >> >> >
> >> >> >> > Didn't we add a UserDict that could be subclassed primarily
> >> >> >> > because
> >> >> >> > subclassing dict directly was such a poor user experience?
> >> >> >> >
> >> >> >> > If adding __missing__ to dict is huge (performance hit?), we
> don't
> >> >> >> > do
> >> >> >> > it.
> >> >> >> > If it's not, I think we should.  Maybe add it to UserDict if
> >> >> >> > performance
> >> >> >> > is
> >> >> >> > a concern?
> >> >> >> >
> >> >> >> >
> >> >> >> > --
> >> >> >> > ~Ethan~
> >> >> >> > _______________________________________________
> >> >> >> > Python-ideas mailing list
> >> >> >> > Python... 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... 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)
> >>
> >>
> >>
> >> --
> >> --Guido van Rossum (python.org/~guido)
>
>
>
> --
> --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/20160705/f658531c/attachment.html>


More information about the Python-ideas mailing list