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

2021年10月06日 07:04:37 -0700

On 06. 10. 21 15:34, Łukasz Langa wrote:
On 6 Oct 2021, at 12:06, Larry Hastings <[email protected] <mailto:[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.
What about this:
group = (KeyboardInterrupt, MemoryError)
other_group = (KeyError, IndexError)
try:
 ...
except group + other_group as error:
 ...
_______________________________________________
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/KH7T6VDRYENBLLFNY7CAXFEVH4IILXZ7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to