python-3.x - 类型注释: Extract inner type from container type

标签 python-3.x type-hinting python-typing type-annotation

我有以下设置:

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

我可以使用什么fooList中获取int

最佳答案

您可以使用属性__args__为了取出int .

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

结果:<class 'int'>

关于python-3.x - 类型注释: Extract inner type from container type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68202140/

相关文章:

python - Mypy子类中更具体的参数

Python列表类型列表串联、随机模块

python - 使用 tkinter 输入一个变量,被调用

python卡住数据类不可变对象(immutable对象).__setattr__

Sklearn CountVectorizer 的 Python 访问标签

python-3.x - Python 类型提示 : self-referential type checking

python - Python 中的类型提示特定字符​​串列表

Python类型提示——为dict子类指定key、value类型

python - 如何注释作为类对象(而不是类实例)的类型?

Python 3.5 类型提示不会导致错误