【问题标题】:Type annotations: Extract inner type from container type类型注释:从容器类型中提取内部类型
【发布时间】:2021-09-13 01:08:42
【问题描述】:

我有以下设置:

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

我可以使用什么foo 来从List 中获取int

【问题讨论】:

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


    【解决方案1】:

    您可以使用属性__args__ 来取出int

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

    结果:<class 'int'>

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    • 2018-01-28
    • 2021-12-11
    • 2020-08-05
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多