[Python-ideas] Match statement brainstorm
Nick Coghlan
ncoghlan at gmail.com
Wed May 25 02:00:00 EDT 2016
On 25 May 2016 at 15:33, Michael Selik <michael.selik at gmail.com> wrote:
> On Tue, May 24, 2016 at 7:03 PM Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
>> But if the right hand side is special, all bets are off
>> and you'll have to explain exactly what would be allowed
>> and how to interpret it.
>
> I agree that catching all exceptions during the assign-if-can (via if/elif
> or switch/case) might be too error-prone. Whether it's the LHS of ``as`` or
> the RHS of ``?=``, it would help to restrict the kind of exceptions
> handled/suppressed as a failed match. However, I fail to see why ``case ...
> as ...`` would be restrictive and ``if ... ?= ...`` would not. They could
> have the same semantics, catching either a specific set of exceptions or
> all/most exceptions.
Having the RHS of an assignment operation be syntactically restricted
isn't something Python has ever done before, while the LHS is already
heavily restricted (since it describes name binding targets rather
than normal expressions for evaluation).
The syntactic restrictions then mean evaluation order can be
controlled to ensure any exception handles only cover the desired step
in the process (e.g. a particular key lookup), rather than the
evaluation of arbitrary subexpressions.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list