4 ms·
> 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 var
by dragonwriter 6y ago
> 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.)
- _nosewings_ 6y agoNot 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.
- dragonwriter 6y ago> 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.)
- pfalcon 6y agoInterested in block-level scoping in Python? Please post on the python-ideas mailing list. Thanks.