我有以下代码:
from typing import Tuple, Dict, List
CoordinatesType = List[Dict[str, Tuple[int, int]]]
coordinates: CoordinatesType = [
{"coord_one": (1, 2), "coord_two": (3, 5)},
{"coord_one": (0, 1), "coord_two": (2, 5)},
]
我想在运行时检查变量是否符合自定义类型定义。 我在想这样的事情:
def check_type(instance, type_definition) -> bool:
return isinstance(instance, type_definition)
但显然isinstance
不起作用。
我需要在运行时检查它,实现它的正确方法是什么
Typeguard
:https://typeguard.readthedocs.io/en/latest/userguide.html#using-type-checker-functions例如:
代码:
结果:
相关问题 更多 >
编程相关推荐