【问题标题】:How to give type annotation for empty set?如何为空集提供类型注释?
【发布时间】:2020-04-25 12:14:42
【问题描述】:

我在 python 中有这一行;

set1 = set()  # create empty set

当我使用 mypi 扫描时,我收到错误 Need type annotation for 'set1'

如何给空集类型注解?

我在 pycharm 中使用 python 3.7 和 mypi 插件。

【问题讨论】:

    标签: python python-3.x static-typing python-typing


    【解决方案1】:

    简单版:set1: set = set()

    更具体的版本:

    from typing import Set
    set1: Set[<type>] = set()
    

    例如对于字符串集:set1: Set[str] = set()

    【讨论】:

      【解决方案2】:

      我不确定什么是需要类型注释,但该注释只是

      set1<b>: set</b> = set()

      【讨论】:

        猜你喜欢
        • 2017-12-21
        • 1970-01-01
        • 2019-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2019-01-07
        • 1970-01-01
        相关资源
        最近更新 更多