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

Michael Selik michael.selik at gmail.com
Wed Jun 29 14:29:48 EDT 2016


On Wed, Jun 29, 2016 at 1:47 PM Matt Gilson <matt at getpattern.com> wrote:

> The following code raises an `AttributeError`:
> >>> class D(dict):
> ...     def __missing__(self, k):
> ...          super(D, self).__missing__(k)
> ...
>
> I find this behavior to be a little bit odd as I would have expected the
> default implementation to have a `__missing__` method
>

The behavior of a dictionary key-lookup checking for __missing__ parallels
the way a standard class does attribute-lookup, checking for __getattr__.

I'm guessing the non-existence of __missing__ on the built-in dict type
(and the non-existence of __getattr__ on the base object type) allows for
some speed optimizations in the interpreter. If so, then the tradeoff of
needing to provide an extra base class shim for your multiple inheritance
hierarchy seems reasonable. Does anyone know the original reason for not
having __missing__ defined on the builtin type?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160629/b41ba093/attachment.html>


More information about the Python-ideas mailing list