How to use match case to check for a variable's type in python?
Asked Answered
W

3

15

I have this code to check whether or not a variable is a number or a Vector2:

def __mul__(self, other):
    match type(other):
        case int | float:
            pass
        case Vector2:
            pass

If I run this, I get

SyntaxError: name capture 'int' makes remaining patterns unreachable

And when I hover in vscode, it gives me:

"int" is not accessed
Irrefutable pattern allowed only as the last subpattern in an "or" pattern
All subpatterns within an "or" pattern must target the same names
Missing names: "float"
Irrefutable pattern is allowed only for the last case statement

If I remove | float it still won't work, so I can't make them separate cases.

Waistline answered 23/4, 2022 at 18:24 Comment(1)
you can take hints from this explanation: stackabuse.com/python-check-if-variable-is-a-numberAbramson
M
23

Case with a variable (ex: case _: or case other:) needs to be the last case in the list. It matches any value, where the value was not matched by a previous case, and captures that value in the variable.

A type can be used in a case, but implies isinstance(), testing to determine if the value being matched is an instance of that type. Therefore, the value used for the match should be the actual variable other rather than the type type(other), since type(other) is a type whose type would match type().

def __mul__(self, other):
    match other:
        case int() | float():
            pass
        case Vector2():
            pass
Maurili answered 31/7, 2022 at 16:26 Comment(1)
This pattern is called a class pattern: "A class pattern provides support for destructuring arbitrary objects". More information can be found in the documentation: peps.python.org/pep-0622/#class-patternsSpalato
C
3

You can add a condition on the pattern:

class A:
    pass

class B:
    pass

class C:
    pass


def check_type(obj: Union[A, B, C]):
    match obj:
        case x if isinstance(x, A):
            return "A"
        case x if isinstance(x, B):
            return "B"
        case x if isinstance(x, C):
            return "C"
        case _:
            return "Unknown"

if __name__ == "__main__":
    a = A()
    b = B()
    c = C()
    print(check_type(a))
    print(check_type(b))
    print(check_type(c))

Output:

A
B
C

See Adding conditions to patterns section of PEP 636.

Cusp answered 22/3, 2024 at 13:13 Comment(1)
I agree - this is definitely the right way to go if you want full type coverage - checking functions using the Callable type, for exampleGoblet
G
0

This also works:

def print_type(v):
    match v:
        # None must come before int
        case None:
            print("NONE")
        # bool() must come before int
        case bool():
            print("BOOL")
        case int():
            print("INT")
        case float():
            print("FLOAT")
        case Vector2():
            print("VECTOR2")
        case str():
            print("STR")

Examples:

print_type(1)  # prints INT
a = 1
print_type(a)  # prints INT
print_type(None)  # prints NONE
print_type("Hello")  # prints STR
v = Vector2()
print_type(v)  # prints VECTOR2

Read more about this from the PEP 622 documentation.

Gillum answered 19/7, 2023 at 10:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.