News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @06:41PM (#26084053)
In that case, what’s the correct way to write a clause that only matches if `status` is equal to 404? Do we have to use the integer literal 404 instead of a named integer?
Re:Pattern matching accepted for Python by ben509 on Tuesday February 09, 2021 @11:21PM (#26085721)
It will be to stick your constants in a module and use module.constant to match them. Or use an enum. Or hang them off a class.
Re:Pattern matching accepted for Python by _nosewings_ on Tuesday February 09, 2021 @07:06PM (#26084260)
Yes, of course. Again, this is how it works in basically every mainstream language with this feature.
Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @07:15PM (#26084325)
Really? It’s best practice to use a magic number rather than a name?

That breaks one of the most fundamental rules for writing good code.

Re:Pattern matching accepted for Python by _nosewings_ on Tuesday February 09, 2021 @07:28PM (#26084411)
If you really need to compare against a variable, use an "if". The primary benefit of pattern-matching is destructuring.

EDIT: It looks like you actually can match against constants with this PEP, as long as you access your constant with a dot (e.g., HttpError.NotFound). This seems like a perfectly reasonable solution to me.

Re:Pattern matching accepted for Python by hnlmorg on Wednesday February 10, 2021 @03:02AM (#26086644)
No it’s not because it’s none obvious and requires a fair amount of boilerplate code. Both of which are usually idioms Python normally tries to avoid.

I guarantee you this will trip up a lot of developers who are either learning the language for the first time or who Python isn’t their primary language.

Worse still, the kind of bugs this will lead to is valid code with unexpected pattern matching, which is a lot harder to debug than invalid code which gets kicked out with a compiler error.

Re:Pattern matching accepted for Python by mannerheim on Wednesday February 10, 2021 @01:49AM (#26086290)
There are PatternSynonyms in Haskell.