【发布时间】:2019-09-16 20:37:34
【问题描述】:
我们可以通过两种方式导入Container:
from collections import Containerfrom collections.abc import Container
两个Container 的help 函数返回相同的文档。
help(collections.Container):
Help on class Container in module collections.abc:
class Container(builtins.object)
| Methods defined here:
|
| __contains__(self, x)
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __subclasshook__(C) from abc.ABCMeta
| Abstract classes can override this to customize issubclass().
|
| This is invoked early on by abc.ABCMeta.__subclasscheck__().
| It should return True, False or NotImplemented. If it returns
| NotImplemented, the normal algorithm is used. Otherwise, it
| overrides the normal algorithm (and the outcome is cached).
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'__contains__'})
help(collections.abc.Container):
Help on class Container in module collections.abc:
class Container(builtins.object)
| Methods defined here:
|
| __contains__(self, x)
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| __subclasshook__(C) from abc.ABCMeta
| Abstract classes can override this to customize issubclass().
|
| This is invoked early on by abc.ABCMeta.__subclasscheck__().
| It should return True, False or NotImplemented. If it returns
| NotImplemented, the normal algorithm is used. Otherwise, it
| overrides the normal algorithm (and the outcome is cached).
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __abstractmethods__ = frozenset({'__contains__'})
这两个导入有什么区别?为什么我们允许两者都做?
更新
从collections (Python 3.7.3) 导入Container 时收到弃用警告。
从Python 3.8不能直接从collections导入。
>>> from collections import Container
main:1: DeprecationWarning: 从 'collections' 而不是从 'collections.abc' 使用或导入 ABC 已被弃用,并且在 3.8 它将停止工作
【问题讨论】:
标签: python-3.x abc