On Wed, Jun 24, 2020 at 11:05 AM Brandt Bucher <brandtbuc...@gmail.com>
wrote:

> Guido van Rossum wrote:
> > IIUC the pattern matching uses either .get(key, <sentinel>) or
> .__contains__(key) followed by .__getitem__(key). Neither of those will
> auto-add the item to a defaultdict (and the Mapping protocol supports
> both). @Brandt: what does your implementation currently do? Do you think we
> need to specify this in the PEP?
>
> I still prefer under-specifying rather than over-specifying, in order to
> allow the implementation some flexibility. Basically, "if you've got a
> funny mapping, it may behave funny".
>
> For example, we currently perform a length check before even getting to
> this point, which is allowed because the spec isn't very strict. This is a
> good thing; we already have a section in the PEP that explicitly allows
> compiler to perform transformations such as C(x) | C(y) -> C(x | y).
>
> If we have to specify it, __contains__ followed by __getitem__ is the way
> to do it. The current behavior is subtly different (and probably wrong),
> but I was going to change it today anyways... I had an outstanding TODO for
> it.
>

So why not  .get(key, <sentinel>)? You can reuse the sentinel, and this way
it's a single call instead of two -- e.g. the code in Mapping implements
both __contains__() and get() by calling __getitem__() and catching
KeyError.

-- 
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him **(why is my pronoun here?)*
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/VPI7CNHSUHP55UCVNDY5AL4FU6WTR2VC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to