If you just pass a type directly, it will consider it to be a "name capture" rather than a "value capture." You can coerce it to use a value capture by importing the builtins module, and using a dotted notation to check for the type.
import builtins
from typing import Type
def main(type_: Type):
match (type_):
case builtins.str: # it works with the dotted notation
print(f"{type_} is a String")
case builtins.int:
print(f"{type_} is an Int")
case _:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
if __name__ == "__main__":
main(type("hello")) # <class 'str'> is a String
main(str) # <class 'str'> is a String
main(type(42)) # <class 'int'> is an Int
main(int) # <class 'int'> is an Int
Answer from Patryk Bratkowski on Stack OverflowIf you just pass a type directly, it will consider it to be a "name capture" rather than a "value capture." You can coerce it to use a value capture by importing the builtins module, and using a dotted notation to check for the type.
import builtins
from typing import Type
def main(type_: Type):
match (type_):
case builtins.str: # it works with the dotted notation
print(f"{type_} is a String")
case builtins.int:
print(f"{type_} is an Int")
case _:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
if __name__ == "__main__":
main(type("hello")) # <class 'str'> is a String
main(str) # <class 'str'> is a String
main(type(42)) # <class 'int'> is an Int
main(int) # <class 'int'> is an Int
As its name suggests, structural pattern matching is more suited for matching patterns, not values (like a classic switch/case in other languages). For example, it makes it very easy to check different possible structures of a list or a dict, but for values there is not much advantage over a simple if/else structure:
if type_to_match == str:
print("This is a String")
elif type_to_match == int:
print("This is an Int")
else:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
But if you really want to use SPM, you could use the guard feature along with issublcass to check if a type is or is a child of any other:
match type_to_match:
case s if issubclass(type_to_match, str):
print(f"{s} - This is a String")
case n if issubclass(type_to_match, int):
print(f"{n} - This is an Int")
case _:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
Videos
This is a common "gotcha" of the new syntax: case clauses are not expressions. That is, if you put a variable name in a case clause, the syntax assigns to that name rather than reading that name.
It's a common misconception to think of match as like switch in other languages: it is not, not even really close. switch cases are expressions which test for equality against the switch expression; conversely, match cases are structured patterns which unpack the match expression. It's really much more akin to generalized iterable unpacking. It asks the question: "does the structure of the match expression look like the structure of the case clause?", a very different question from what a switch statement asks.
For example:
t = 12.0
match t:
case newvar: # This is equal to `newvar = t`
print(f"bound a new variable called newvar: {newvar}")
# prints "bound a new variable called newvar: 12.00000000"
# this pattern matches anything at all, so all following cases never run
case 13.0:
print("found 13.0")
case [a, b, c]: # matches an iterable with exactly 3 elements,
# and *assigns* those elements to the variables `a`, `b` and `c`
print(f"found an iterable of length exactly 3.")
print(f"these are the values in the iterable: {a} {b} {c}")
case [*_]:
print("found some sort of iterable, but it's definitely")
print("not of length 3, because that already matched earlier")
case my_fancy_type(): # match statement magic: this is how to type check!
print(f"variable t = {t} is of type {my_fancy_type}")
case _:
print("no match")
So what your OP actually does is kinda like this:
t = 12.0
tt = type(t) # float obviously
match tt:
case int: # assigns to int! `int = tt`, overwriting the builtin
print(f"the value of int: {int}")
# output: "the value of int: <class 'float'>"
print(int == float) # output: True (!!!!!!!!)
# In order to get the original builtin type, you'd have to do
# something like `from builtins import int as int2`
case float: # assigns to float, in this case the no-op `float = float`
# in fact this clause is identical to the previous clause:
# match anything and bind the match to its new name
print(f"match anything and bind it to name 'float': {float}")
# never prints, because we already matched the first case
case float(): # since this isn't a variable name, no assignment happens.
# under the hood, this equates to an `isinstance` check.
# `float` is not an instance of itself, so this wouldn't match.
print(f"tt: {tt} is an instance of float") # never prints
# of course, this case never executes anyways because the
# first case matches anything, skipping all following cases
Frankly, I'm not entirely sure how the under-the-hood instance check works, but it definitely works like the other answer says: by defintion of the match syntax, type checks are done like this:
match instance:
case type():
print(f"object {instance} is of type {type}!")
So we come back to where we started: case clauses are not expressions. As the PEP says, it's better to think of case clauses as kind of like function declarations, where we name the arguments to the function and possibly bind some default values to those newly-named arguments. But we never, ever read existing variables in case clauses, only make new variables. (There's some other subtleties involved as well, for instance a dotted access doesn't count as a "variable" for this purpose, but this is complicated already, best to end this answer here.)
Lose the type() and also add parentheses to your types:
t = 12.0
match t:
case int():
print("int")
case float():
print("float")
I'm not sure why what you've wrote is not working, but this one works.
Hello,
I'm trying to use SPM to test if some variable is an int or a str and I can't seem to make it work.
The following code:
from typing import Type
def main(type_to_match: Type):
match type_to_match:
case str():
print("This is a String")
case int():
print("This is an Int")
case _:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
if __name__ == "__main__":
test_type = str
main(test_type)Outputs: https://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg
I fail to see what I'm doing wrong.
EDIT: A very helpful SO user found a solution. Here it is, if you're interseted:
import builtins
from typing import Type
def main(type_: Type):
match (type_):
case builtins.str: # it works with the dotted notation
print(f"{type_} is a String")
case builtins.int:
print(f"{type_} is an Int")
case _:
print("\nhttps://en.meming.world/images/en/0/03/I%27ve_Never_Met_This_Man_In_My_Life.jpg")
if __name__ == "__main__":
main(type("hello")) # <class 'str'> is a String
main(str) # <class 'str'> is a String
main(type(42)) # <class 'int'> is an Int
main(int) # <class 'int'> is an Int