On 08/05/2022 13:08, Valentin Berlier wrote:
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)
I am strongly against special cases for match (or anything else). I
think a walrus assignment should behave like any other sub-expression,
and it's behaviour should be consistent across different contexts.
The walrus expression would evaluate to None if the pattern on the left can't
be matched.
print(x := 42) # 42
This is already legal.
print(1 := 42) # None
Sorry, I don't understand this or see the point of it. Surely trying to
assign to a literal should remain a syntax error.
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)
See above - shouldn't try to assign to a literal.
Best wishes,
Rob Cliffe
_______________________________________________
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/HC7TAUYFTDMP52KAGDJFIB27KFSSI6C3/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
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/GQBCHUSPDMXCRBCULXQXHIDYZF37XURF/
Code of Conduct: http://python.org/psf/codeofconduct/