Python-检查变量是否为类型化类型
有没有办法检查变量是否为给定的类型 我的意思是这样的:Python-检查变量是否为类型化类型,python,python-typing,Python,Python Typing,有没有办法检查变量是否为给定的类型 我的意思是这样的: 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]])
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
有一个名为type的Python函数 i、 e
这应该对你有帮助。另外,当我第一次看到这个问题时,pydanticI也会给出类似的答案,但问题更复杂:
for item_list in list:
type(item_list)