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

Ethan Furman ethan at stoneleaf.us
Wed Jun 29 13:50:56 EDT 2016


On 06/29/2016 10:47 AM, Matt Gilson wrote:

> The following code raises an `AttributeError`:
>
>  >>> class D(dict):
> ...     def __missing__(self, k):
> ...          super(D, self).__missing__(k)
> ...
>  >>> d = D()
>  >>> d['key']
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>    File "<stdin>", line 3, in __missing__
> AttributeError: 'super' object has no attribute '__missing__'
>
> I find this behavior to be a little bit odd as I would have expected the
> default implementation to have a `__missing__` method and I would expect
> the `__missing__` method to raise an appropriate `KeyError`.  I think
> that this would facilitate a class hierarchy where each class can decide
> "I don't know hot to handle this missing key, maybe something further up
> the MRO does".

+1

--
~Ethan~



More information about the Python-ideas mailing list