News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by AlexandrB on Tuesday February 09, 2021 @04:01PM (#26082339)
Don't those languages typically have immutable variables so you would not be able to rebind a constant by accident?
Re:Pattern matching accepted for Python by the_duke on Tuesday February 09, 2021 @04:05PM (#26082373)
This is usually orthogonal to mutability.

In other languages that support match, whether functional or not, you are not changing the value of the variable, but you are shadowing/rebinding the identifier inside the scope of the match clause.

Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @04:10PM (#26082419)
Python `match` statements do not introduce a new scope.

The second example does indeed change the value of `not_found`.

Re:Pattern matching accepted for Python by mbo on Tuesday February 09, 2021 @05:30PM (#26083303)
What's the rationale for not introducing a new scope?

The only clause I can find in the PEP is

> A capture pattern always succeeds. It binds the subject value to the name using the scoping rules for name binding established for named expressions in PEP 572. (Summary: the name becomes a local variable in the closest containing function scope unless there's an applicable nonlocal or global statement.)

This seems... incredibly bad.

Re:Pattern matching accepted for Python by magnusmundus on Tuesday February 09, 2021 @05:38PM (#26083401)
It's consistent with how scoping works in loops and `with` clauses. Agreed that it's more problematic here, though.
Re:Pattern matching accepted for Python by deathanatos on Tuesday February 09, 2021 @05:52PM (#26083587)
That works the same as the rest of the language, doesn't it?

(That is, for, if, while, etc. also don't introduce new scopes.)

Re:Pattern matching accepted for Python by pdw on Tuesday February 09, 2021 @05:44PM (#26083479)
"Introducing a new scope" is not a concept that exists in Python, you only have function scope and module scope.
Re:Pattern matching accepted for Python by CogitoCogito on Tuesday February 09, 2021 @07:39PM (#26084482)
Having only function, module, class, generator, etc. scope in Python before this PEP might have made sense, but they really should have added pattern matching scope to keep things sane here.
Re:Pattern matching accepted for Python by pansa2 on Tuesday February 09, 2021 @05:47PM (#26083521)
There are some exceptions to this - for example, the iteration variable(s) in a list comprehension are only in scope within the comprehension.
Re:Pattern matching accepted for Python by amirkdv on Tuesday February 09, 2021 @11:50PM (#26085839)
Worth noting that this was _fixed_ in Python 3.

In Python 2.7 you get this:

    >>> my_list = [x for x in 'hello']
    >>> x
    'o'
Re:Pattern matching accepted for Python by pdw on Tuesday February 09, 2021 @06:14PM (#26083803)
Thanks, I didn't realize that. That makes their choice really baffling.
Re:Pattern matching accepted for Python by _nosewings_ on Tuesday February 09, 2021 @07:34PM (#26084454)
The explanation is that (in Python 3) list comprehensions are really just syntactic sugar for generator expressions. In Python, "scope" is really a synonym for "dictionary attached to some object", so generators can have local variables (since they have their own internal scope), but purely syntactic constructs cannot.
Re:Pattern matching accepted for Python by remram on Tuesday February 09, 2021 @06:13PM (#26083794)
and class scope
Re:Pattern matching accepted for Python by baby on Tuesday February 09, 2021 @06:42PM (#26084058)
So you can easily create a variable in a match case
Re:Pattern matching accepted for Python by rwmj on Tuesday February 09, 2021 @04:56PM (#26082884)
Oh dear, that is indeed very bad.
Re:Pattern matching accepted for Python by the_duke on Tuesday February 09, 2021 @05:33PM (#26083345)
That's a very problematic design decision, imo...
Re:Pattern matching accepted for Python by danielscrubs on Wednesday February 10, 2021 @02:05AM (#26086361)
Doesn’t other languages have block scope and shadowing is not an issue, but in Python it will reassign instead of shadowing?

In my mind that is not well thought out.

Re:Pattern matching accepted for Python by pfalcon on Wednesday February 10, 2021 @05:24AM (#26087491)
Interested in block-level scoping in Python? Please post on the python-ideas mailing list. Thanks.
Re:Pattern matching accepted for Python by baby on Tuesday February 09, 2021 @06:41PM (#26084052)
In Rust no