在学习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个?你知道吗
您正在查看模块的} 在这里被列为
__all__
导出的全局变量列表。这个列表比文档化的对象要大,原因有两个。第一个是它包含子模块;在本例中^{abc
。你知道吗第二个是为了向后兼容而添加的对象;额外的名称来自
collections.abc
子模块。很久以前,模块不存在,那些对象在移动之前就存在于collections
本身中。来自该模块的名称被导入collections
,以容纳仍然从旧位置导入的代码。你知道吗这是documented on the page you linked:
在这26个中,有一个是
collections
的子模块,collections.abc
,剩下的25个对象显式地支持向后兼容移动到collections.abc
模块的名称。你知道吗如果您想了解其他对象,那么您会发现它们被记录在自己的位置。使用} module documentation 。你知道吗
help(collections.abc)
或^{相关问题 更多 >
编程相关推荐