使用typing模块,可以指定任意嵌套类型,例如List[str]
或Dict[str, Dict[str, float]]
。有没有办法确定对象的类型是否与这种类型匹配?类似的东西
>>> from typing import List
>>> isinstance(['a', 'b'], List[str])
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "/home/cbieganek/anaconda3/lib/python3.6/typing.py", line 1162, in __instancecheck__
# return issubclass(instance.__class__, self)
# File "/home/cbieganek/anaconda3/lib/python3.6/typing.py", line 1148, in __subclasscheck__
# raise TypeError("Parameterized generics cannot be used with class "
# TypeError: Parameterized generics cannot be used with class or instance checks
我真的没想到会isinstance()
为此工作,但我想知道是否有其他可接受的方式来做这件事。