模块集合中的其他26个元素`

2024-04-26 00:58:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在学习Python的过程中,我正在努力往更深的地方挖掘,往更高的地方爬。你知道吗

在尝试学习collections模块的所有知识时,我在探索它的每个角落时都遇到了问题。 在联机文档和help(collections)中,它介绍了9种专门的容器数据类型,请参见 8.3. collections — Container datatypes

['Counter', 'OrderedDict', 'defaultdict', 'deque',
 'namedtuple', 'ChainMap', 'UserDict', 'UserList', 'UserString']

与该列表相比,文档中没有指定的其他26个列表在help()输出中的详细信息非常有限:

x = [ i for i in dir(collections) if not i.startswith('_')]
>>> list(enumerate(x, start=1))
[(1, 'AsyncGenerator'), (2, 'AsyncIterable'), (3, 'AsyncIterator'), (4, 'Awaitable'), (5, 'ByteString'), (6, 'Callable'), 
 (7, 'ChainMap'), (8, 'Collection'), (9, 'Container'), (10, 'Coroutine'), (11, 'Counter'), (12, 'Generator'), (13, 'Hashable'), (14, 'ItemsView'), (15, 'Iterable'), (16, 'Iterator'), 
 (17, 'KeysView'), (18, 'Mapping'), (19, 'MappingView'), (20, 'MutableMapping'), (21, 'MutableSequence'), (22, 'MutableSet'),
 (23, 'OrderedDict'), (24, 'Reversible'), (25, 'Sequence'), (26, 'Set'), (27, 'Sized'), (28, 'UserDict'), 
 (29, 'UserList'), (30, 'UserString'), (31, 'ValuesView'), 
 (32, 'abc'), (33, 'defaultdict'), (34, 'deque'), (35, 'namedtuple')]

是否有必要花时间探索其他26个?你知道吗


Tags: 文档列表container地方counterhelpnamedtuplecollections
1条回答
网友
1楼 · 发布于 2024-04-26 00:58:14

您正在查看模块的__all__导出的全局变量列表。这个列表比文档化的对象要大,原因有两个。第一个是它包含子模块;在本例中^{}在这里被列为abc。你知道吗

第二个是为了向后兼容而添加的对象;额外的名称来自collections.abc子模块。很久以前,模块不存在,那些对象在移动之前就存在于collections本身中。来自该模块的名称被导入collections,以容纳仍然从旧位置导入的代码。你知道吗

这是documented on the page you linked

Changed in version 3.3: Moved Collections Abstract Base Classes to the collections.abc module. For backwards compatibility, they continue to be visible in this module as well.

在这26个中,有一个是collections的子模块, collections.abc,剩下的25个对象显式地支持向后兼容移动到collections.abc模块的名称。你知道吗

如果您想了解其他对象,那么您会发现它们被记录在自己的位置。使用help(collections.abc)^{} module documentation。你知道吗

相关问题 更多 >