I've found an unexpected behaviour for Python structural pattern matching that I want to discuss today.
All the code there is run with CPython 3.10.8
So, let's take a look on the code below
match str(True):
case str(True): print(1)
case str(False): print(2)
case _: print(3)
I expect this code to print 1. The reason for that is that str(True) will evaluate into "True" in both match part and case part. I expect "True" to match "True". However, surprisingly for me, this code print 3.
We can also rewrite this into this piece of code:
match str(True):
case "True": print(1)
case "False": print(2)
case _: print(3)
This time this code will print 1.
What happening there? Why python pattern match working differently depending on whether there is an expression after "case" word or not. Should evaluations be forbidden in pattern-matching? What is rationale behind this?