Python: Acceptance of Pattern Matching – PEP 634
mail.python.org
Python: Acceptance of Pattern Matching – PEP 634
1–10 of 14 posts
Re: Python: Acceptance of Pattern Matching – PEP 634
#2Accepted:
Specification https://www.python.org/dev/peps/pep-0634/
Motivation and Rationale https://www.python.org/dev/peps/pep-0635/
Tutorial https://www.python.org/dev/peps/pep-0636/
Rejected:
Unused variable syntax https://www.python.org/dev/peps/pep-0640/
Explicit Pattern Syntax for Structural Pattern Matching https://www.python.org/dev/peps/pep-0642/
Re: Python: Acceptance of Pattern Matching – PEP 634
#3https://discuss.python.org/t/gauging-sentiment-on-pattern-ma...
Re: Python: Acceptance of Pattern Matching – PEP 634
#4 match status:
case 404:
return "Not found"
not_found = 404
match status:
case not_found:
return "Not found"
IIRC in an earlier proposal, the former would check for equality (`status == 404`) and the latter would perform an assignment (`not_found = status`).Re: Python: Acceptance of Pattern Matching – PEP 634
#5It's interesting that pattern matching has been accepted despite a poll showing a majority (65%) of core developers opposed to this particular proposal, and a plurality (44%) opposed to pattern matching altogether. https://discuss.python.org/t/gauging-sentiment-on-pattern-ma...
Re: Python: Acceptance of Pattern Matching – PEP 634
#6It's interesting that pattern matching has been accepted despite a poll showing a majority (65%) of core developers opposed to this particular proposal, and a plurality (44%) opposed to pattern matching altogether. https://discuss.python.org/t/gauging-sentiment-on-pattern-ma...
Re: Python: Acceptance of Pattern Matching – PEP 634
#7It's interesting that pattern matching has been accepted despite a poll showing a majority (65%) of core developers opposed to this particular proposal, and a plurality (44%) opposed to pattern matching altogether. https://discuss.python.org/t/gauging-sentiment-on-pattern-ma...
I'm not sure if you can read the 'accept 634 + another' options as opposed to 'accept 634' (that said you can't necessarily read it as in support of it either. This poll is a bit of a mess in that regard). But it's certainly contentious (all options including accepting 634 still only comes to 50%).
However, I’d say that “accept 634 + another” is clearly opposed to “accept 634 & reject both others”, which is the decision that has been made.
Re: Python: Acceptance of Pattern Matching – PEP 634
#8Is it still the case that these two pieces of code have different behaviour? match status: case 404: return "Not found" not_found = 404 match status: case not_found: return "Not found" IIRC in an earlier proposal, the former would check for equality (`status == 404`) and the latter would perform an assignment (`not_found = status`).
Edit: It is possible to use constants or variables in a case pattern, but they have to be dotted names to not be treated like a capture pattern. So this would work for your example:
class HttpStatus(Enum):
NOT_FOUND = 404
match status:
case HttpStatus.NOT_FOUND:
return "Not found"Re: Python: Acceptance of Pattern Matching – PEP 634
#9Earlier quoted context omitted.
I'm not sure if you can read the 'accept 634 + another' options as opposed to 'accept 634' (that said you can't necessarily read it as in support of it either. This poll is a bit of a mess in that regard). But it's certainly contentious (all options including accepting 634 still only comes to 50%).
> I'm not sure if you can read the 'accept 634 + another' options as opposed to 'accept 634' However, I’d say that “accept 634 + another” is clearly opposed to “accept 634 & reject both others”, which is the decision that has been made.
Re: Python: Acceptance of Pattern Matching – PEP 634
#10Is it still the case that these two pieces of code have different behaviour? match status: case 404: return "Not found" not_found = 404 match status: case not_found: return "Not found" IIRC in an earlier proposal, the former would check for equality (`status == 404`) and the latter would perform an assignment (`not_found = status`).
Yes, the variable names in the case patterns are treated like variables on the left hand side of an assignment statement. Whatever value they may have had before is irrelevant. A pattern which consists solely of a variable name is a wildcard pattern that matches anything, just as it would be on the left side of an assignment. Edit: It is possible to use constants or variables in a case pattern, but they have to be do…
def _id(x): return x
not_found = 404
match status:
case _id(not_found):
return "Not found"