Pattern-matching is great. I think PEP 634 is on the right track, but it
would be a waste to only use pattern-matching for choosing a branch in a
match statement.
Let’s look at Rust:
if let [x, y] = my_array {
...
}
Rust "if let" constructs are an alternative to full-blown match
statements that make it less verbose to match a single pattern.
We have a similar problem. The syntax proposed by PEP 634 is pretty
verbose for matching a single pattern:
match my_list:
case [x, y]:
...
Two keywords and two indentation levels. We can do better:
if [x, y] := my_list:
...
Yes, your first intuition is right. This looks similar to the Rust
version but would work completely differently. But hear me out. Let's
look past my terrible example and focus on the idea.
1. The walrus operator was purposefully designed to create bindings and
not to perform assignments to arbitrary lvalues.
2. Matching a pattern only introduces bindings as well, no assignments.
3. The current behavior of the walrus operator is equivalent to matching
the right-side operand to an "irrefutable" capture pattern.
Allowing the walrus operator to do pattern-matching would simply make
the returned value conditional. If the pattern doesn't match, the walrus
operator returns None.
print(x := 42) # 42
print(1 := 42) # None
The current PEG parser would backtrack when encountering the walrus
operator to interpret the left-side as a pattern.
Finally, more examples:
# Buy every pizza on the menu
sum(
price for food in menu
if ({"type": "pizza", "price": price} := food)
)
# Download all images from document
{url: download(url) for tag in html if (Img(src=url) := tag)}
# Monitor service health
while Response(status=200, json={"stats": stats}) := health_check():
print(stats)
time.sleep(5)
I'm convinced that making the walrus operator a pattern-matching
operator would turn it into the perfect companion for PEP 634. What do
you think?
References:
- PEP 634: https://www.python.org/dev/peps/pep-0634/
- Rust "if let": https://doc.rust-lang.org/book/ch06-03-if-let.html
--
Valentin
_______________________________________________
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/MJ7JHYKHKB2T4SCFV4TX4IMKUANUAF5B/
Code of Conduct: http://python.org/psf/codeofconduct/