News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by airstrike on Tuesday February 09, 2021 @05:25PM (#26083247)
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:Pattern matching accepted for Python by kortex on Tuesday February 09, 2021 @11:16PM (#26085691)
I dig it. 'as' primes my brain to something going into a scoped context.
Re:Pattern matching accepted for Python by Spivak on Wednesday February 10, 2021 @12:34AM (#26086009)
It’s fine. Python just introduced expression assignment. Why not use that?

    match subject
        case [first := _, rest := _]
            return [rest, first]
Re:Pattern matching accepted for Python by kortex on Wednesday February 10, 2021 @10:44AM (#26090662)
That works too but I guess I am still getting used to the walrus operator. What really bugs me about this PEP is they are going through all this effort to introduce match but they don't have match assignments, which is personally one of my favorite things about match.
Re:Pattern matching accepted for Python by collyw on Wednesday February 10, 2021 @03:50AM (#26086892)
Well get to Perl eventually
Re:Pattern matching accepted for Python by pfalcon on Wednesday February 10, 2021 @05:04AM (#26087369)
Note that the original PEP622 relied on ":=" initially, but they found some corner case with it (apparently, operator precedence related) and switched to "as" in PEP634.
Re:Pattern matching accepted for Python by ziml77 on Wednesday February 10, 2021 @12:12AM (#26085926)
I'm still wary about this change making it in to Python, but I like this suggestion. It makes the assignment clear. The way it's currently specified would definitely trip me at some point.
Re:Pattern matching accepted for Python by neillyons on Friday February 12, 2021 @04:46AM (#26112008)
I like your proposal. The `as` keyword is very readable to me.

I would have preferred `as` instead of the walrus operator too.

Re:Pattern matching accepted for Python by baby on Tuesday February 09, 2021 @06:40PM (#26084045)
This would be really confusing to anyone who is used with match statements in other languages
Re:Pattern matching accepted for Python by coldtea on Tuesday February 09, 2021 @09:35PM (#26085166)
For 2 seconds until they learn what it means. If anything, it's more explicit what exactly it does.

There are 100s of things in Python who would be confusing to people used to other languages, this is not one of them.

Re:Pattern matching accepted for Python by reikonomusha on Wednesday February 10, 2021 @01:23AM (#26086194)
The general tripping up of binding vs mutation vs assignment vs initialization is a pervasive Python issue. This just continues to double down on exacerbating the problem.
Re:Pattern matching accepted for Python by hnlmorg on Wednesday February 10, 2021 @02:44AM (#26086557)
Comparing garbage syntax to worse syntax and saying “it’s not as bad as that” isn’t the way one improves upon a language.
Re:Pattern matching accepted for Python by coldtea on Wednesday February 10, 2021 @03:51AM (#26086901)
Which is neither here, nor there.

This syntax is clearly an improvemen to the PEP syntax.

And parent said it's "confusing". Well, it's less confusing than the PEP syntax.

Nobody compared it to not adding it at all, or was concerned with improving the language aside from the pattern matching case here.

Re:Pattern matching accepted for Python by hnlmorg on Wednesday February 10, 2021 @04:14AM (#26087045)
Almost everyone in this discussion is making exactly those comparisons. Including yourself. When you're discussing usability issues due to changes to the syntax, the perspective of non-exclusive developers vs full time Python devs doesn't change the underlying context of the discussion regarding the change in usability.

And I stand by my position that defending a bad decision because of the existence of worse decisions is a really piss poor counterargument.

Disclaimer: I'm a language designer myself so I know first hand how hard it is to get these things right.

Re:Pattern matching accepted for Python by coldtea on Wednesday February 10, 2021 @06:12AM (#26087780)
>And I stand by my position that defending a bad decision because of the existence of worse decisions is a really piss poor counterargument.

This thread was just about the two alternatives (the PEP and explicit capture), not about the PEP in general, or about defending the PEP or even saying that the better alternative is "good". We just say it's better than the PEP. Not sure how you read that into what we wrote.

>Disclaimer: I'm a language designer myself so I know first hand how hard it is to get these things right.

Then go make that argument in some thread in this post discussing the PEP proposal in general?

Re:Pattern matching accepted for Python by hnlmorg on Wednesday February 10, 2021 @11:37AM (#26091387)
> This thread was just about the two alternatives (the PEP and explicit capture), not about the PEP in general, or about defending the PEP or even saying that the better alternative is "good". We just say it's better than the PEP. Not sure how you read that into what we wrote.

Bullshit. You said, and I quote, "There are 100s of things in Python who would be confusing". That's what I was responding to. And my point stands: just because there are other pain points in Python doesn't mean we should accept more pain points into it.

> Then go make that argument in some thread in this post discussing the PEP proposal in general?

I’d prefer to make that argument in the thread where I was already talking you about the PEP proposal in general. ;)

Re:Pattern matching accepted for Python by lolinder on Wednesday February 10, 2021 @02:01AM (#26086340)
But the existing semantics are very confusing to anyone who is used to Python, which seems like a bigger problem.
Re:Pattern matching accepted for Python by baby on Wednesday February 10, 2021 @03:38PM (#26094418)
I don't agree, I'm used to Python and this is not confusing to me because I have learned to use match statements, you will do too.