3 ms·
What a disaster. Breaking the rule that literals and variables are equivalent as r-values is terrible for the language. Python already has the walrus operator —
by bobbylarrybobby 6y ago
What a disaster. Breaking the rule that literals and variables are equivalent as r-values is terrible for the language. Python already has the walrus operator — it’s a real shame that they decided not to leverage it for disambiguating binding and matching in pattern matching.
match status_code
case NOT_FOUND:=_ :
print(“clearly we’re binding a variable here”)
case _:=NOT_FOUND:
print(“and here we’re matching”)
- amai 6y agoThere is a discussion on this issue here: https://github.com/gvanrossum/patma/issues/140 https://github.com/gvanrossum/patma/issues/140
- breuleux 6y agoPretty much all languages that implement pattern matching treat case statements as l-values, not r-values. `match x: case y: ...` basically means "execute `y = x`, enter the body if it succeeds, try the next case if it fails". Python does allow more things as a case than it does in an assignment, but with the exception of `case enum.NOT_FOUND` (which IMO is a mistake and should not have been allowed), it could use the exact same syntax for `y = x` and `case y` if it wanted to, and it would be perfectly logical. In the programming language I made a while back, my match statement was similar to Python's, and every case statement was also a valid l-value, so you could write `Point(x, y) | (x, y) = something` or `123 = x`, which would fail unless `x == 123`.