1

Is there a way to check if a variable is a given typing type?

I mean something like this:

def check_type(variable: Any, typing: Any) -> bool:
    return variable is typing


check_type([1, 2, 3], List[int])  # True
check_type([1, 2, 3.4], List[int])  # False
check_type([1, 2, 3.4], List[Union[int, float]])  # True
Myzel394
  • 1,155
  • 3
  • 16
  • 40

1 Answers1

-1

There is Python function called type()

i.e.

for item_list in list:
    type(item_list)
Zaharskyy
  • 99
  • 3
  • 10
  • I was going to give a similar answer when I first saw the question, but what is being asked is more complicated: https://docs.python.org/3/library/typing.html – Derek O May 26 '20 at 02:07