with the addition of PEP 634 and the new match/case syntax in python 3.10, it
seems fitting to be able to use these new match statements inside of
expressions as well, which is why I think we should have a new `matches`
keyword (similar to if/else, is, for, not, etc.) for use in expressions. This
makes many more complex if/else statements much less complex (see example use
cases at #2), and overall looks much more pythonic in practice. The expression
would be higher on the order of operations than the boolean operators, but
higher than the comparisons, but this is subject to change. (see #1) However,
this should be used sparingly (ideally for single comparisons), similar to list
comprehensions versus for loops.
1. GRAMMAR MODIFICATIONS:
```
inversion:
| 'not' inversion
| match_expression
matches_expression:
| comparison 'matches' patterns # patterns is defined in PEP 634
| comparison
```
2. USE CASES:
a.
```
if variable matches ["example", *files]:
print(files)
```
as opposed to:
```
match variable:
case ["example", *files]:
print(files)
```
b.
```
for x in some_list:
if x matches Class1(attr1=3) | Class2(attr2=4):
print("got a match!")
break
```
as opposed to
```
for x in some_list:
match x:
case Class1(attr1=3) | Class2(attr2=4):
print("got a match!")
break
```
___
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/FINMYEQ4A4MCG72U4PXJGZ3VCJLA75VD/
Code of Conduct: http://python.org/psf/codeofconduct/