>
> Reading between the lines, I *think* that you want the match statement
> to catch the exception that you get when the attribute lookup fails, am
> I right?
Yes!
I was hoping there could be some syntax to extend pattern matching to
handle exceptions such that we could handle patterns with multiple types of
exceptions like so:
match *this_raises_an_exception*, *this_raises_another_exception*:
case *AttributeError*, *TypeError*:
print("catches attribute and type errors")
case *AttributeError*, *AttributeError*:
print("catches attribute and attribute")
case *Exception*, *Exception*:
print("catches the remaining exceptions")
case *x*, *y*:
print(f"{x} and {y}")
case *_*, *_*:
print("everything else")
Any thoughts on this kind of syntax?
Maybe the author could explicitly distinguish that an exception might be
raised by using *with Exception *like so:
match *this_raises_an_exception*, *this_raises_another_exception** with
Exception:*
On Mon, Jan 3, 2022 at 7:25 PM Steven D'Aprano <[email protected]> wrote:
> Hi Elvis,
>
>
> On Sat, Jan 01, 2022 at 12:59:32AM -0500, elvis kahoro wrote:
>
> > The functionality that I'm thinking about is:
> >
> > match (named_tuple_object.*missing_attribute*, a_random_string):
> > case *AttributeError*, "Catching an attribute error":
> > print("Catches as attribute error")
> > case *err:= AttributeError*, "Assigns an attribute error as err":
> > print(f"This is the captured attribute error: {*err*}")
>
> Reading between the lines, I *think* that you want the match statement
> to catch the exception that you get when the attribute lookup fails, am
> I right?
>
> The problem here is that exceptions are values that can already be
> matched, and the regular pattern matching rules apply:
>
>
> >>> spam = (AttributeError, "eggs")
> >>> match spam:
> ... case (Exception, str):
> ... print("matched")
> ...
> matched
>
>
> So `case Exception` is going to match the exception as a class or
> instance. We would need new syntax to match a *raised* exception.
>
> I propose:
>
> match expression:
> except exceptions:
> block
> # regular cases follow after the except block
>
> which will only catch exceptions raised when evaluating the match
> expression. That is, equivalent to:
>
> try:
> temp = expression
> except exceptions:
> block
> else:
> match temp:
> # regular cases follow here
>
> except that there is no actual "temp" variable created.
>
> To be clear, the except block must come first, ahead of all the cases.
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list -- [email protected]
> To unsubscribe send an email to [email protected]
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/[email protected]/message/WUUGAQAUBHXLVJFV6JFGXZH7VBIX24K3/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/KGPMOW6CM46FUBUN6YPSC4NTY6WQWJNU/
Code of Conduct: http://python.org/psf/codeofconduct/