> Why should a failed match return None? That's not helpful if it matches
> but the value itself is None.
The only pattern that would match `None` is this one:
print(None := get_value()) # Always None
Here, the walrus operator would always return `None`. Either because the
function returned `None` or the function returned something else and the
pattern didn't match. The behavior is consistent, this particular pattern is
just not that useful. Pattern-matching shouldn't exempt you from checking for
`None` with the `is` operator anyway:
print(get_value() is None) # True or False
The proposed semantics don't get in the way of idiomatic python. Most of the
time you only care about the truthiness of the value returned by the walrus
operator. The most common example would be with regular expressions:
price_tag = "Price: $7"
if match := re.match(r".*(\d+).*", price_tag):
print(match[1])
By the way I'm hoping that with PEP 634 `Match` objects can become proper
`collections.abc.Sequence` instances. This would allow regex destructuring:
if [_, amount] := re.match(r".*(\d+).*", price_tag):
print(amount)
--
Valentin
_______________________________________________
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/7GWNFOLPAASUZCXAXAAUWFX7APVGNM4X/
Code of Conduct: http://python.org/psf/codeofconduct/