Live data from Hacker News

Python: Acceptance of Pattern Matching – PEP 634

mail.python.org

11–14 of 14 posts

Re: Python: Acceptance of Pattern Matching – PEP 634

#11
post #4

Is 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, and this is incredibly disappointing.

Couldn't we achieve the same functionality with a little less ambiguity using the following syntax?:

    not_found = 404
    match status:
        case not_found:
            return "Not found"
            
        case _ as foo:
            do_something(foo)
            return "Match anything"
it even works for the example in PEP 365

    match json_pet:
        case {"type": "cat", "name": _ as name, "pattern": _ as pattern}:
            return Cat(name, pattern)
        case {"type": "dog", "name": _ as name, "breed": _ as breed}:
            return Dog(name, breed)
        case _:
            raise ValueError("Not a suitable pet")

Re: Python: Acceptance of Pattern Matching – PEP 634

#12
post #3

It'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...

Don't forget that the poll is dated 2020-11-20 (and majority of votes were cast soon). There was a lot of discussion (and thus maybe elaboration of opinions) since that.

Re: Python: Acceptance of Pattern Matching – PEP 634

#13
post #4

Is 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, and this is incredibly disappointing. Couldn't we achieve the same functionality with a little less ambiguity using the following syntax?: not_found = 404 match status: case not_found: return "Not found" case _ as foo: do_something(foo) return "Match anything" it even works for the example in PEP 365 match json_pet: case {"type": "cat", "name": _ as name, "pattern": _ as pattern}: return Cat(name, pattern) case…

I'm with everyone on "incredibly disappointing" valuation. But being pragmatic, I had to accept the idea "it can be addressed later" vs continuing this mess. And there was truly mess, e.g. PEP642, which initially tried to addressed just this issue, turned in a hairy monster trying to random-patch pattern matching considerably in rather weird ways.

Re: Python: Acceptance of Pattern Matching – PEP 634

#14
post #4

Is 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`).

I don’t understand this. How are we supposed to read the case word? Is there an online interpreter for this so we can play with it?
Post reply on HN