[Python-Dev] Re: PEP 654 except* formatting

2021年10月06日 06:38:31 -0700

> On 6 Oct 2021, at 12:06, Larry Hastings <[email protected]> wrote:
> 
> It seems like, for this to work, "group" would have to become a keyword.
> 
No, just like `match` and `case` didn't have to.
> This would play havoc with a lot of existing code.
> 
Extraordinary claims require extraordinary evidence, Larry. I maintain this 
will be entirely backwards compatible.
> Even making it a soft keyword, a la "await" in 3.5, would lead to ambiguity:
> 
> group = KeyboardInterrupt
> 
> try:
> while True:
> print("thou can only defeat me with Ctrl-C")
> except group as error:
> print("lo, thou hast defeated me")
> 
Two things:
1. This is a convoluted example, I bet 100ドル you won't find such an `except 
group` statement in any code predating my e-mail 🤠 Sure, sometimes (very 
rarely) it's useful to gather exceptions in a variable. But I'm pretty sure 
`group` won't be the name chosen for it.
2. While non-obvious, the example is not ambiguous. There can only be one 
parsing rule fitting this:
'except' expression 'as' NAME ':'
Note how this is different from:
'except' 'group' expression 'as' NAME ':'
There could be confusion if except-star, whatever its name is going to be, 
supported an empty "catch all" variant like `except:`. Thankfully, this is 
explicitly listed as a no-go in PEP 654. So `except group:` remains 
unambiguous. We can even make its error message smarter than the default 
NameError, since -- as I claim -- it's terribly unlikely somebody would mean to 
name their dynamic exception collection "group".
- Ł

Attachment: signature.asc
Description: Message signed with OpenPGP

_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/OQUS7X5D3WXYN4WKFRHIBHG25XDA247G/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to