typing
模块(或任何其他模块)展示一个 API 以在运行时对变量进行类型检查,类似于 isinstance()
但了解 typing
中定义的类型类?
我想做一些类似于:
from typing import List
assert isinstance([1, 'bob'], List[int]), 'Wrong type'
最佳答案
我正在寻找类似的东西并找到了图书馆 typeguard .这可以在任何你想要的地方自动进行运行时类型检查。还支持直接检查问题中的类型。从文档中,
from typeguard import check_type
# Raises TypeError if there's a problem
check_type('variablename', [1234], List[int])
关于 python >=3.5 : Checking type annotation at runtime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646823/