On Sat, Oct 31, 2020 at 21:47 Steven D'Aprano <st...@pearwood.info> wrote:

> On Sat, Oct 31, 2020 at 09:05:43PM -0700, Guido van Rossum wrote:
>
> > Hm, for PEP 622/634 we looked at this and ended up making it so that this
> > is the default -- you only have to write
> > ```
> > {'spam': spam, 'eggs': eggs} = mapping
> > ```
> > and any extra keys are ignored. This is because for the common use case
> > here we want to ignore extra keys, not insist there aren't any.
>
> Regardless of which is commoner than the other, what about the use-case
> where you do want to insist that the mapping is an exact match? Having
> matched the two keys how do I say that there are no more keys?


You add ‘**rest’ and a guard ‘if not rest’.

>
In the case of the `match` statement, I think that "ignore extra keys"
> by default is risky. Consider something like this:
>
>     match mapping:
>         case {'spam': spam}:
>              print(spam)
>         case {'spam': spam, 'eggs': eggs}:
>              print('this will never be called')
>
> There's no case that will match the second that isn't already captured
> by the first.


That falls under the general heading of “put more specific cases first”, so
you should swap the cases. (Similar rules apply to class patterns and Are
familiar from except clauses.)

> --
--Guido (mobile)
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FGOTL7X25MGG5XQZDBIBJXLZJ3OFI4Z2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to