Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix TypeError: called match pattern must be a type in Python 3.10

Trying to learn Python 3.10 pattern matching. Tried this example after reading 8.6.4.9. Mapping Patterns

>>> match 0.0:
...  case int(0|1):
...   print(1)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: called match pattern must be a type
>>>

Especially the note on built-in types, including int. How should I code to test for an integer value of 0 or 1 (the example in the doc) and not get this error?

like image 415
user1443098 Avatar asked Jun 14 '26 12:06

user1443098


1 Answers

I fell into a gotcha:

match 0.0
  case int:
    print(1)

effectively redefines int, so the next time I tried the match I posted, it failed since my int was shadowing the built in

like image 163
user1443098 Avatar answered Jun 18 '26 01:06

user1443098