21

I came across a weird issue while using the new match/case syntax in Python3.10. The following example seems like it should work, but throws an error:

values = [
    1,
    "hello",
    True
]

for v in values:
    match type(v):
        case str:
            print("It is a string!")
        case int:
            print("It is an integer!")
        case bool:
            print("It is a boolean!")
        case _:
            print(f"It is a {type(v)}!")
$ python example.py
  File "/.../example.py", line 9
    case str:
         ^^^
SyntaxError: name capture 'str' makes remaining patterns unreachable
  • It is mentioning that the first case (the value str) will always result in True.

Wondering if there is an alternative to this other than converting the type to a string.

3
  • case is followed by a pattern. Identifiers in patterns are variables that will be assigned from the corresponding part of the matching value, not they're not variables that are evaluated.
    – Barmar
    May 18, 2022 at 20:51
  • So case str: means that if the value is a single value, assign the value to str and execute that case body. It doesn't mean if type(v) == str:
    – Barmar
    May 18, 2022 at 20:51
  • You seem to be confusing this with switch/case from PHP or JavaScript.
    – Barmar
    May 18, 2022 at 20:52

2 Answers 2

26

Rather than match type(v), match v directly:

values = [
    1,
    "hello",
    True,
]

for v in values:
    match v:
        case str():
            print("It is a string!")
        case bool():
            print("It is a boolean!")
        case int():
            print("It is an integer!")
        case _:
            print(f"It is a {type(v)}!")

Note that I've swapped the order of bool() and int() here, so that True being an instance of int doesn't cause issues.

This is a class pattern match.

9
  • 1
    what about performance of instantiating new object for simple comparison?
    – iperov
    Jan 6 at 12:14
  • 3
    @iperov Type matching in case does not actually instantiate a new object. You can see this for yourself by defining a custom type and putting a print in the init - it won't be called.
    – wim
    Jan 6 at 19:04
  • This fails for numpy integer types.
    – Soerendip
    Jan 25 at 18:17
  • @Soren Can not reproduce. Could you show an example?
    – wim
    Jan 25 at 18:24
  • take numpy.int64(1) as input
    – Soerendip
    Jan 25 at 19:36
-2

quotation marks

values = [ False, "hello", True ]

for v in values: match type(v): case 'str': print("It is a string!") case 'int': print("It is an integer!") case 'bool': print("It is a boolean!") case _: print(f"It is a {type(v)}!")

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 26 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.