[Python-ideas] should `dict` supply a default `__missing__` implementation?
Émanuel Barry
vgr255 at live.ca
Wed Jun 29 15:04:32 EDT 2016
> From: Guido van Rossum
> To: Michael Selik
> Cc: Python-Ideas
> Subject: Re: [Python-ideas] should `dict` supply a default `__missing__`
> implementation?
>
> The argument against adding `dict.__missing__()` is that it would just
> a shortcut to raise KeyError with certain parameters.
Can't the interpreter optimize that out? Something like 'if
ns["__missing__"] is dict.__missing__: raise KeyError' ? I figure it
wouldn't be hard, doesn't Python already do stuff like that in a bunch of
other places?
> 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__`.
The difference is that `dict` explicitly supports __missing__ for
subclasses, so that users don't have to re-define __getitem__ for those
purposes. I don't know of many classes explicitly supporting a given feature
but not implementing at least a method for it that does nothing. But maybe
that's just me.
-Emanuel
More information about the Python-ideas
mailing list