satwikkansal / wtfpython

What the f*ck Python? 😱
Do What The F*ck You Want To Public License
35.7k stars 2.65k forks source link

Structural Pattern Matching Regular Expression WTF #298

Open wyuenho opened 2 years ago

wyuenho commented 2 years ago
import re
DIGIT = re.compile(r"\d")

match "1":
    case DIGIT as i:
        print(i) # >>> 1

DIGIT # >>> '1' WTF!?

The reason is DIGIT is treated as a capture pattern, and the variable is thus reassigned in the case clause. DIGIT is assigned to "1" first, and then another variable i was created and the value of DIGIT, which now holds "1", is assigned to it.

PEP 634 does not define regular expression as a valid pattern.