问题描述
我们可以通过两种方式导入容器:
We can import Container in two ways:
- 从collections导入容器
- 从collections.abc导入容器
- from collections import Container
- from collections.abc import Container
help 函数对于两个 Container 返回相同文档。
help function for both Container returns the same documentation.
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容器):
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__'})
这两种进口之间有什么区别?为什么我们允许两者都做?
What is the difference between these two imports? Why are we allowed to do both?
更新
导入容器时出现弃用警告来自集合( Python 3.7.3 )。
从 Python 3.8 起,不能直接从集合导入。
From Python 3.8 it cannot be imported directly from collections.
>>> from collections import Container
推荐答案
来自:
From the Python 3 documentation for the collections module:
这些集合抽象基类当前包括
AsyncGenerator , AsyncIterable , AsyncIterator , Awaitable ,
字节串,可调用, Collection ,容器,协程,
生成器,可哈希, ItemsView ,可迭代 ,迭代器, KeysView ,
映射, MappingView , MutableMapping , MutableSequence ,
MutableSet ,可逆,序列,设置,大小, ValuesView 。
These "Collections Abstract Base Classes" currently includeAsyncGenerator, AsyncIterable, AsyncIterator, Awaitable,Bytestring, Callable, Collection, Container, Coroutine,Generator, Hashable, ItemsView, Iterable, Iterator, KeysView,Mapping, MappingView, MutableMapping, MutableSequence,MutableSet, Reversible, Sequence, Set, Sized, ValuesView.
在Python 3.8中,从集合导入它们将停止工作。
在Python 3.3至3.7中,可以从 collections 导入,也可以从 collections.abc >(给出完全相同的类)。
在Python 3.7中,从 collections 导入它们会打印
弃用警告,因为Python 3.8即将发布。
In Python 3.8 importing them from collections will stop working.In Python 3.3 to 3.7, they can be imported from collections orfrom collections.abc (it gives the exact same classes).In Python 3.7, importing them from collections prints adeprecation warning, since Python 3.8 is getting near.
在Python 2中,它们只能从'collections'导入,
不能从'collections.abc'导入。
In Python 2 they can only be imported from 'collections',not from 'collections.abc'.
一种简单的方法处理此问题是一个try / except块:
One simple way to deal with this is a try/except block:
try: # works in Python >= 3.3 from collections.abc import Sequence except ImportError: # Python 2, Python <= 3.2 from collections import Sequence
另一个常用的解决方法是有条件地从 collections 或 collections.abc >
取决于所使用的Python版本。
Another commonly used workaround is to conditionallyimport from collections or collections.abc dependingon the Python version being used.
例如,使用 PY2 布尔值并执行:
For instance, have a PY2 boolean and do:
if PY2: from collections import Sequence else: from collections.abc import Sequence
此布尔值通常是obtai可以使用六个:
This boolean is usually obtained either using six:
from six import PY2
或使用 sys.version_info :
import sys PY2 = int(sys.version_info[0]) == 2
如果我们预计Python 4在这方面可能会像Python 3.3+
一样工作,那么特殊外壳的Python 2似乎更具前瞻性
比特殊外壳的Python 3可以完成以下操作:
If we anticipate that Python 4 is likely to work like Python 3.3+in this respect, special-casing Python 2 seems more future-proofthan special-casing Python 3, which could be done as follows:
if PY3: from collections.abc import Sequence else: from collections import Sequence
其中 PY3 布尔值可以使用六个来获取:
where the PY3 boolean can be obtained either using six:
from six import PY3
或使用 sys .version_info :
import sys PY3 = int(sys.version_info[0]) == 3
上面的try / except方法似乎更尽管
仍然很健壮(例如它可以轻松地与Python 3.2一起使用)。
The try/except approach above seems even more robust though(e.g. it works with Python 3.2 with no extra effort).
这篇关于来自集合导入容器和来自collections.abc导入容器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!