Steven D'Aprano writes:

 > I propose:
 > 
 >     match expression:
 >         except exceptions:
 >             block
 >         # regular cases follow after the except block

I probably would rarely use this syntax (preferring the explicit
temporary, and possibly encapsulating the exception handling in a
separate function) because in my common use cases for match (currently
using if-elif-else), the exceptions are somebody else's issue that I
have to defend against and there are a lot of them which almost all
need some special casing, and what I care about are the match cases,
which I want as close to the expression as possible.  Ie, I would do
something like

    def compute_specific_expression()
        try:
            return expression
        except OneException as e:
            handle_it_1(e)
        except TwoException as e:
            handle_it_2(e)

    expression_value = compute_specific_expression()
    match expression_value:
        case ...

where the long names are intended to express the meaning of the
expression in some detail, in preference to

    match expression:
        except OneException as e:
            handle_it_1(e)
        except TwoException as e:
            handle_it_2(e)
        case ...

Also, we don't have to assume that match has try semantics (ie, a
prologue of code which raises exceptions for match's handlers to
catch).  We could define match handlers to only catch exceptions from
the expression, and just say that if you want to handle exceptions
from all the cases in a uniform way, wrap the match in a try.  This
allows you to distinguish expression exceptions from case exceptions
in the same way your syntax does, but allows me to satisfy my desire
to have the cases at the top.

Yes this would be confusing to people who would assume that handlers
apply to all code in the match's suite, and I don't blame them for
being confused.  But IMO the benefits of putting the handlers at the
end outweigh the costs of learning that the syntax means what it
means.

Steve


_______________________________________________
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/X6YTHKS7VCUX2VW2QQCI3LQGI4DE5KEE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to