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/