Sets模块弃用警告

26

当我运行我的Python脚本时,我会得到以下的警告

DeprecationWarning: the sets module is deprecated

我该如何解决这个问题?


Python的哪个版本? - Aaron Digulla
5个回答

35

停止使用 sets 模块,或者切换到一个旧版本的 Python,因为在较新的版本中这个模块已被弃用。

根据 PEP-0004,从 v2.6 开始,sets 已经被弃用,被内置的 setfrozenset 类型所替代


4
修复引起警告的问题,以解决该警告。这看起来非常简单。+1 - S.Lott
2
只有当你知道有一个内置的替代它时,它才显得简单。为什么警告信息没有说呢?! - GreenAsJade

27

历史:

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

5

如果你想修复它,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模块。

来自文档

sets模块已被弃用;最好使用内置的setfrozenset类型。


2

您不需要导入sets模块就可以使用它们,因为它们在内置命名空间中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接