"Structural pattern matching" for Python, part 2
"Structural pattern matching" for Python, part 2
Posted Sep 3, 2020 7:06 UTC (Thu) by NYKevin (subscriber, #129325)In reply to: "Structural pattern matching" for Python, part 2 by nybble41
Parent article: "Structural pattern matching" for Python, part 2
How exactly would you define "reasonable?"
IMHO, the Python rule is very straightforward and easy to remember. Moreover, I've never run into a situation where it actually causes a problem of some sort. Sure, variables hang around for a little longer than they would otherwise, but so what? If you really care, you can explicitly write del x, which is probably a Good Idea anyway, because if you care, then you should call attention to the fact that you care (not-caring is the default state of affairs, so x must be special in some way).
> Making it harder to mess around with frame objects seems like a feature, not a bug. Making them completely inaccessible would be even better.
And while we're at it, let's put the e in creat(2), where it should've been all along.
You can't break backcompat on a feature just because you personally dislike that feature. It exists. It needs to be supported. Unless we want to do *another* 3.x-style flag day, frame objects are here to stay.
> I think that could be avoided by simply defaulting to function scope as current Python does when assigning to an unknown variable.
As I mentioned in the comments the last time we discussed this idea:
- There is no such thing as an "unknown" variable, except at the global scope.
- It is always possible to determine at compile time which variables belong to which scopes, with the exception of the global scope. The bytecode compiler will then emit bytecode that only consults the single scope where the variable actually lives, or consults the global scope if the variable is not recognized.
- The global scope is not known at compile time, for a number of reasons, most obviously the fact that from foo import * is legal at the global scope (it's a SyntaxError anywhere else). This doesn't matter, because the "unknown = global" algorithm is good enough for the current design.
- Because the global scope is not known at compile time, it is not possible to determine at compile time whether an unrecognized variable in a match expression is a global or a nonexistent variable that should be created and bound to the case's scope.
- Therefore, allowing match expressions to create a new scope does not actually solve this problem, and would introduce a significant amount of complexity for no adequately justified reason.
