【问题标题】:Sets module deprecated warning设置模块不推荐使用的警告
【发布时间】:2010-01-11 08:25:12
【问题描述】:

当我运行我的 python 脚本时,我收到以下警告

DeprecationWarning: the sets module is deprecated

我该如何解决这个问题?

【问题讨论】:

  • 哪个版本的 Python?

标签: python


【解决方案1】:

停止使用sets 模块,或切换到未弃用的旧版python。

根据pep-004sets 自 v2.6 起已弃用,取而代之的是内置的 set and frozenset types

【讨论】:

  • +1:通过修复导致警告的问题来修复警告。看起来很简单。
  • 如果你知道有一个内置的替代它,它看起来很简单。为什么警告不这么说!?
【解决方案2】:

历史:

在 Python 2.3 之前:没有设置功能
Python 2.3:sets 模块到达
Python 2.4:引入了 setfrozenset 内置函数
Python 2.6:sets 模块已弃用

您应该将代码更改为使用set 而不是sets.Set

如果您仍希望能够支持使用 Python 2.3,您可以在脚本的开头执行此操作:

try:
   set
except NameError:
   from sets import Set as set

【讨论】:

    【解决方案3】:

    如果你想修复它,James 肯定有正确的答案,但如果你只想关闭弃用警告,你可以像这样运行 python:

    $ python -Wignore::DeprecationWarning 
    Python 2.6.2 (r262:71600, Sep 20 2009, 20:47:22) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sets
    >>> 
    

    (来自:http://puzzling.org/logs/thoughts/2009/May/3/python26-deprecation-warning

    您也可以通过编程方式忽略它:

    import warnings
    warnings.simplefilter("ignore", DeprecationWarning)
    

    【讨论】:

      【解决方案4】:

      使用内置的set,而不是导入和使用sets 模块。

      来自documentation

      sets 模块已被弃用; 最好使用内置集 和frozenset类型。

      【讨论】:

        【解决方案5】:

        您无需导入 sets 模块即可使用它们,它们位于内置命名空间中。

        【讨论】:

          猜你喜欢
          • 2020-01-31
          • 1970-01-01
          • 2020-05-14
          • 1970-01-01
          • 2020-02-23
          • 2019-09-21
          • 2016-09-19
          • 2015-02-26
          • 2017-03-01
          相关资源
          最近更新 更多