A while ago there was a discussion about allowing "match" patterns for the
walrus operator. This would cover iterable unpacking as you suggested along
with all the patterns allowed in match statements.
if [x, y, z] := re.match(r"...").groups():
print(x, y, z)
The walrus expression would evaluate to None if the pattern on the left can't
be matched.
print(x := 42) # 42
print(1 := 42) # None
This would make it really useful in if statements and list comprehensions. Here
are a couple motivating examples:
# Buy every pizza on the menu
cost_for_all_pizzas = sum(
price for food in menu
if ({"type": "pizza", "price": price} := food)
)
# Monitor service health
while Response(status=200, json={"stats": stats}) := health_check():
print(stats)
time.sleep(5)
_______________________________________________
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/HC7TAUYFTDMP52KAGDJFIB27KFSSI6C3/
Code of Conduct: http://python.org/psf/codeofconduct/