【问题标题】:Python - check if variable is a typing type [duplicate]Python - 检查变量是否是类型[重复]
【发布时间】:2020-05-23 07:41:09
【问题描述】:

有没有办法检查变量是否是给定的类型?

我的意思是这样的:

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

【问题讨论】:

标签: python python-typing


【解决方案1】:

有一个 Python 函数叫做 type()

for item_list in list:
    type(item_list)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2018-10-06
  • 2021-10-26
  • 2017-12-15
  • 2018-06-16
  • 1970-01-01
相关资源
最近更新 更多