I think we've fallen in the trap of assuming we all agree on the meaning of "scope".

In Python, that word is typically used to refer to a bunch of behaviors that go together. In particular, "the current scope" is where assignment creates new variables. (It is also linked to the semantics of "return" and "yield", and to "global" and "nonlocal".) The paper uses it in this sense.

The problem with using scopes *in this sense* for case blocks is that all assignments to variables inside a case block would create the new variable in the scope of that case block. But consider this example:
```
match arg:
  case Point(x, y):
    p = (x, y)
  case (x, y):
    p = (x, y)
  case _:
    raise TypeError
print(p)
```
There are no assignments to p outside the case blocks. Giving each case block a traditional "scope" would prevent the print(p) call outside the match statement from seeing the variable p. (Please don't argue that this should be refactored by putting the print call in a helper function.)

Now, of course we could change the language so that this all works. But it would require a lot of changes to the compiler, since it gives scopes a bundle of semantics that would have to be separated out.

We could also agree to keep the language the same and use "scope" with a different meaning (the meaning that you seem to want). But you can't blame the paper for using it in the way it is used, since that is the conventional usage of the term for Python.


On Tue, Nov 17, 2020 at 7:03 AM Mark Shannon <mark@hotpy.org> wrote:
Hi,

It turns out that implementing the save and restore semantics in the
example I gave is not that difficult.

I was motivated to find out by the DLS 2020 paper on pattern matching.
It claims that introducing small scopes for variables would have to be
implemented as a function preventing the use of normal control flow.

Since restoring the variable after an except block is a similar problem,
I thought I'd see how difficult it was.

If anyone's interested, here's a prototype:

https://github.com/python/cpython/compare/master...markshannon:fix-exception-scoping

(This only saves globals and function-locals, class-locals and
non-locals are unchanged. I'd probably want to emit a syntax warning for
non-locals, as the semantics are a bit weird).

Cheers,
Mark.

On 17/11/2020 9:55 am, Mark Shannon wrote:
> Hi,
>
> I'm wondering why
> ```
> x = "value"
> try:
>      1/0
> except Exception as x:
>      pass
> ```
>
> does not restore "value" to x after
> the `except` block.
>
> There doesn't seem to be an explanation for this behavior in the docs or
> PEPs, that I can find.
> Nor does there seem to be any good technical reason for doing it this way.
>
> Anyone know why, or is just one of those unintended consequences like
> `True == 1`?
>
>
> Here's an example of restoring the value of the variable after the
> `except` block:
>
>  >>> def f(x):
> ...     try:
> ...         1/0
> ...     except Exception as x:
> ...         pass
> ...     return x
> ...
>  >>> f("hi")
> 'hi'
>
>
> Cheers,
> Mark.
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/KGYRLITEPB22ZZO4N7DD4A7QP7FQS6JO/
>
> Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/IAY4XHLOOA572INPMP34WYXZPOSORBYU/
Code of Conduct: http://python.org/psf/codeofconduct/


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)