I submitted the main link as the "tutorial" page (PEP 636), since I think it's the best overview of this new structure and how it will work. Multiple other relevant links: Message to the mailing...
I submitted the main link as the "tutorial" page (PEP 636), since I think it's the best overview of this new structure and how it will work. Multiple other relevant links:
I think it must be because I'm used to the situation where the "case" identifier introduces a new scope hiding the outer variable rather than assigning it.
I think it must be because I'm used to the situation where the "case" identifier introduces a new scope hiding the outer variable rather than assigning it.
At least coming from elixir, I don’t see a problem with this. The case some_variable simply matches any value and binds it to the variable some_variable. Semi-realistic code for handling http...
At least coming from elixir, I don’t see a problem with this. The case some_variable simply matches any value and binds it to the variable some_variable.
Semi-realistic code for handling http responses:
response={:ok,%{status:404,body:"Not Found"}}caseresponsedo:error->"network error"{:ok,%{status:200}}->"ok"# this will match and bind {:ok, %{status: 404, body: "Not Found"}} to `other`other->log_bad_response(other)"bad response"end
It might help if Python’s match construct had something like elixir’s pin operator ^ which lets you reference the value of a previously bound variable in a match case instead of binding to it.
foo="hello"bar="world"casebardo^foo->"will not match since \"world\" != \"hello\""foo->"matches and rebinds foo to \"world\" within the scope of this match clause"end
Edit: I think I see what you meant now. In the linked example does the rebinding happen outside the scope of the pattern match? If so then yeah, that’s not great.
I submitted the main link as the "tutorial" page (PEP 636), since I think it's the best overview of this new structure and how it will work. Multiple other relevant links:
Python 3.10 is scheduled to release in October 2021, so this is still relatively far in the future.
This comment on Hacker News highlights a gotcha which is not at all intuitive and alarming (to me).
Many languages with pattern matching don't even have mutation. One would expect it to define a new variable scoped to the pattern match.
I think it must be because I'm used to the situation where the "case" identifier introduces a new scope hiding the outer variable rather than assigning it.
At least coming from elixir, I don’t see a problem with this. The case
some_variable
simply matches any value and binds it to the variablesome_variable
.Semi-realistic code for handling http responses:
It might help if Python’s match construct had something like elixir’s pin operator
^
which lets you reference the value of a previously bound variable in a match case instead of binding to it.Edit: I think I see what you meant now. In the linked example does the rebinding happen outside the scope of the pattern match? If so then yeah, that’s not great.