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

Guido van Rossum guido at python.org
Tue Jul 5 18:19:19 EDT 2016


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)


More information about the Python-ideas mailing list