4 ms·
IMO the match statement has some very unintuitive behaviour: match status: case 404: return "Not found" not_found = 404 match
by pansa2 5y ago
IMO the match statement has some very unintuitive behaviour:
match status:
case 404:
return "Not found"
not_found = 404
match status:
case not_found:
return "Not found"
The first checks for equality (`status == 404`) and the second performs an assignment (`not_found = status`).
`not_found` behaving differently from the literal `404` breaks an important principle: “if you see an undocumented constant, you can always name it without changing the code’s meaning” [0].
[0] https://twitter.com/brandon_rhodes/status/1360226108399099909 https://twitter.com/brandon_rhodes/status/136022610839909990...
- DangitBobby 5y agoI see. Is it fair to say your issue with the feature is less about not wanting the feature and more about the implementation details?
- pansa2 5y agoActually, I don’t really want the feature. It’s complicated and it doesn’t really fit with the rest of the language (it breaks fundamental rules, as above, and has scoping issues). Worst of all, though, it’s really just another way to write if-elif-else chains that call `isinstance` - a pattern which Python traditionally discouraged in favour of duck-typing.
- dataflow 5y agoAw dang you spoiled it :-) I was hoping my example would be more fun to work through haha.