[issue45323] unexpected behavior on first match case _

Joël Bourgault report at bugs.python.org
Wed Sep 29 11:19:21 EDT 2021


New submission from Joël Bourgault <joel.bourgault at gmail.com>:

While testing the `match...case` construction, I get the following behavior with Docker image Python 3.10 rc2-slim:

```python
>>> match "robert":
...     case x if len(x) > 10:
...         print("long nom")
...     case [0, y]:
...         print("point à x nul")
...     case _:
...         print("nothing interesting")
...
nothing interesting
>>> x  # assigned, since matched, even if 'guarded-out'
'robert'
>>> y  # not assigned, because not matched
Traceback (most recent call last):
...
NameError: name 'y' is not defined
>>> _  # normally not assigned, but we get a value?? 😱
'robert'
>>> del _  # but the variable does not even exist!?!?!? 😱😱😱
Traceback (most recent call last):
...
NameError: name '_' is not defined

```

Moreover, if we continue working in the same session by assigning `_` explicitly and playing with `case _`, we don't get any weird behavior anymore, and `_` behaves as a normal variable.

So it seems to me that there is some weird corner case here, that should be adressed.

----------
messages: 402884
nosy: ojob
priority: normal
severity: normal
status: open
title: unexpected behavior on first match case _
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45323>
_______________________________________


More information about the Python-bugs-list mailing list