类型注释:从容器类型中提取内部类型

问题描述

我有以下设置:

from typing import List
a = List[int]
foo(a) == int

我可以用什么fooint中获取List

解决方法

您可以使用属性 __args__ 来去掉 int

from typing import List
a = List[int]
print(a.__args__[0])

结果:<class 'int'>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...