容器是否总是可迭代的? [英] Are containers always iterable?
本文介绍了容器是否总是可迭代的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据该图,是否存在容器不可迭代的情况?
Is there a scenario where the container is not iterable, according to this graph?
推荐答案
取决于您总是的意思。根据 collections.abc
Depends on what you mean by always. According to collections.abc
- 一个容器是实现
__ contains __
方法的对象 - 可迭代的对象是实现
__ iter __
(或__ getitem __
,作为后备)
- a container is an object that implements
__contains__
method - an iterable is an object that implements
__iter__
(or__getitem__
, as a fallback)
因此,从理论上讲,没有,您可以实现一个不可迭代的容器。但是,所有标准python容器(以及大多数由库实现的容器)也是可迭代的。
So, theoretically, no, you can implement a container that is not an iterable. However, all standard python containers (and most containers implemented by libraries) are also iterable.
这篇关于容器是否总是可迭代的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文