News for hackers. Stuff that matters.
Re:Pattern matching accepted for Python by dragonwriter on Tuesday February 09, 2021 @07:21PM (#26084362)
> This is how pattern matching works in any language

No, its not, but no language (at least that I am aware of) except python does pattern matching + local variables + not introducing a new scope with the pattern match.

Ruby is the closest, but it does introduce a new scope while providing a mechanism for binding variables in the containing local scope. (As well as a method to “pin” variables from the containing scope to use them in matches.)

Re:Pattern matching accepted for Python by _nosewings_ on Tuesday February 09, 2021 @08:13PM (#26084727)
Not introducing a new scope with a match is unfortunate, but it's also consistent with how every other language feature interacts with scoping.

> (As well as a method to “pin” variables from the containing scope to use them in matches.)

This is a good idea, I agree -- at least for Python, where you would obviously just call __eq__.

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 dragonwriter on Wednesday February 10, 2021 @06:04AM (#26087722)
> Not introducing a new scope with a match is unfortunate, but it's also consistent with how every other language feature interacts with scoping.

Except comprehensions, which changed in Py 3 to have their own scope, rather than binding control variables in the surrounding (function or module) scope as in Py 2.

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

It would be except:

* You can't access function-scoped identifiers that way.

* You can't access module-scoped identifiers in the main module that way.

* You can't conveniently reference identifiers in the current module that way. (I think you can use the full module path to qualify the name in the local module, but that's both awkward and brittle to refactoring, and there's pretty much never a reason to do it for any other purpose.)

Re:Pattern matching accepted for Python by pfalcon on Wednesday February 10, 2021 @05:28AM (#26087518)
Interested in block-level scoping in Python? Please post on the python-ideas mailing list. Thanks.