简体   繁体   English

将iterable作为参数的函数是否总是接受迭代器?

[英]Does a function which takes iterable as parameter always accept iterator?

I know iterator is iterable , but only 1 pass. 我知道iteratoriterable ,但只有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 an iterable , 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 on iterables but actually doesn't work with iterators . 野外的一些代码声称可以处理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内置的序列类型的示例包括strunicodelisttuplebytearraybufferxrange


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. 出于您的目的, ContainerSized可能是最有用的,因为它们分别需要__contains____len__ ,而这又需要一组定义良好的值,这些值可以重复迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接受 1 个可迭代参数并添加列表中的所有对象(如果它们是 int 或 float)的函数? - Function that takes 1 iterable parameter and adds all objects in the list if they are int or float? 为什么带参数的函数在迭代器中无法调用? - Why function which takes arguments cannot callable in iterator? python-重复可迭代对象元素的迭代器 - python - Iterator which duplicates elements of iterable object 如何记录将函数作为Sphinx参数的函数? - How can I document a function which takes a function as a parameter with Sphinx? 可迭代和迭代器 - Iterable and iterator 编写一个函数来验证它作为参数的数字是否是阿姆斯特朗数 - Write a function which verifies if the number it takes as parameter is an Armstrong number or not Python:带有可迭代参数并生成元组的函数 - Python :function that takes an iterable argument and generates tuples Function 接受可选参数 - Function that takes in optional parameter 接受迭代器并返回迭代器中第一个可被 2 整除的元素的函数 - function that takes an iterator and returns the first element in the iterator that is divisible for 2 如何将 MyPy 作为参数接受的类型提示传递给内部定义函数的函数? - How can I pass a type hint that MyPy will accept as a parameter to a function which defines a function internally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM