[英]Does a function which takes iterable as parameter always accept iterator?
I know iterator
is iterable
, but only 1 pass. 我知道
iterator
是iterable
,但只有1遍。
For example, many functions in itertools
take iterable
as parameter, eg islice
. 例如,
itertools
许多函数将iterable
作为参数,例如islice
。 Can I always pass in iterator
if I see the api says iterable
? 如果我看到api说
iterable
iterator
话,我可以一直传入iterator
吗?
As @delnan pointed out: 正如@delnan指出:
Although every
iterator
is aniterable
, some people (outside the core team) say "iterable" when they mean "something that can be iterated several times with with the same results".虽然每个
iterator
都是iterable
,但有些人(在核心团队之外)说“可迭代”,而他们的意思是“可以用相同的结果迭代几次”。 Some code in the wild claims to work oniterables
but actually doesn't work withiterators
.野外的一些代码声称可以处理
iterables
但实际上不适用于iterators
。
That is exact my concern. 这是我的担忧。 Is there a name for those
iterable
which support multipass? 是否有支持多
iterable
名称? Like IEnumerable
in C#? 喜欢C#中的
IEnumerable
?
If I am to build a function which claims to support iterable
, is it best practice to actually support iterator
as well? 如果我要构建一个声称支持
iterable
的函数,那么最好还是实际支持iterator
吗?
Yes, the functions in itertools are designed for use with iterators. 是的,itertools中的函数设计用于迭代器。 The reason why the function signatures say
iterable
is because they also work on lists, tuples, and other iterables which are not iterators. 函数签名说
iterable
的原因是因为它们也适用于列表,元组和其他不是迭代器的迭代。
A sequence is an iterable which supports efficient element access using integer indices via the __getitem__()
special method and defines a len()
method that returns the length of the sequence. 序列是一个可迭代的,它通过
__getitem__()
特殊方法使用整数索引支持有效的元素访问,并定义一个返回序列长度的len()
方法。
This definition is slightly different than the set of all iterables which are not iterators. 此定义与不是迭代器的所有迭代集的集合略有不同。 (You could define a (crippled) custom class which has a
__getitem__
, method but not a __len__
. It would be an iterable which is not an iterator -- but neither would it be a sequence
.) (你可以定义一个(残缺的)自定义类,它有一个
__getitem__
,方法但不是__len__
。它将是一个迭代,它不是一个迭代器 - 但它也不是一个sequence
。)
However sequences
is pretty close to what you are looking for, since all sequences are iterables which can be iterated over multiple times. 然而,
sequences
与您正在寻找的非常接近,因为所有序列都是可迭代的迭代,可以多次迭代。
Examples of sequence types built into Python include str
, unicode
, list
, tuple
, bytearray
, buffer
and xrange
. Python内置的序列类型的示例包括
str
, unicode
, list
, tuple
, bytearray
, buffer
和xrange
。
Here are some definitions culled from the glossary : 以下是从词汇表中挑选出来的一些定义:
container
Has a __contains__ method
generator
A function which returns an iterator.
iterable
An object with an __iter__() or __getitem__() method. Examples of
iterables include all sequence types (such as list, str, and
tuple) and some non-sequence types like dict and file. When an
iterable object is passed as an argument to the builtin function
iter(), it returns an iterator for the object. This iterator is
good for one pass over the set of values.
iterator
An iterable which has a next() method. Iterators are required to
have an __iter__() method that returns the iterator object
itself. An iterator is good for one pass over the set of values.
sequence
An iterable which supports efficient element access using integer
indices via the __getitem__() special method and defines a len()
method that returns the length of the sequence. Note that dict
also supports __getitem__() and __len__(), but is considered a
mapping rather than a sequence because the lookups use arbitrary
immutable keys rather than integers. sequences are orderable
iterables.
deque is a sequence, but collections.Sequence does not recognize
deque as a sequence.
>>> isinstance(collections.deque(), collections.Sequence)
False
Yes, because every iterator is also an iterable. 是的,因为每个迭代器也是可迭代的。
An object is iterable if it defines the __iter__()
method. 如果对象定义
__iter__()
方法,则该对象是可迭代的。 Every iterator has this method, it returns the iterator itself. 每个迭代器都有这个方法,它返回迭代器本身。
You should look at the abstract base classes defined in the collections
module . 您应该查看
collections
模块中定义的抽象基类。 For your purposes, Container
or Sized
might be the most useful, as they require __contains__
and __len__
respectively, which in turn require a well defined set of values that could be iterated over repeatedly. 出于您的目的,
Container
或Sized
可能是最有用的,因为它们分别需要__contains__
和__len__
,而这又需要一组定义良好的值,这些值可以重复迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.