迭代器vs生成器vs可迭代 [英] Iterators vs Generators vs Iterables
问题描述
我是Python
的新手.我能够理解Iterables
和Iterators
.
但是我已经看到有很多东西可以比较Generators
与Iterators
.
I am newbie to Python
. I was able to understand Iterables
and Iterators
.
However I have seen that there is lot of stuff that compares Generators
vs Iterators
.
据了解,Iterable
是一个对象,实际上内部存储有元素(例如,列表).他们遵循迭代协议,在其中实现了__iter__()
方法,该方法返回一个Iterator
对象,该对象有助于迭代Iterable
.
As per understanding, Iterable
is an object which actually has elements stored inside it (E.g. a list). They follow an iteration protocol where they implement __iter__()
method which returns an Iterator
object which helps in iterating the Iterable
.
根据我的理解,Generators
有助于动态生成数据,而不是在内存中创建大数据结构并返回它.我们也可以通过使用Iterators
达到类似的目标.
As per my understanding Generators
helps in generating the data on the fly instead of creating a big data structure in memory and returning it. We can achieve simialr goal by the use of Iterators
as well.
现在,我很怀疑,如果我们已经有了Iterators
,那么Generators
的需求是什么,因为两者都有助于实现即时生成数据的相似目标.
这仅仅是为了简化语法,还是Generators
存在的其他原因?
Now my doubt, If we already had Iterators
what was the need of Generators
, since both helps acheiving a similar goal of generating data on the fly.
Is that just to simplify the syntax or is there any other reason why Generators
exist ?
推荐答案
Here's how these terms are defined in the glossary in the official Python documentation.
可迭代
一个能够一次返回其成员的对象.示例 可迭代对象包括所有序列类型(例如
list
,str
和tuple
)和某些非序列类型,例如dict
,file
对象和 您使用__iter__()
方法或定义的任何类的对象 实现序列语义的__getitem__()
方法.
An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as
list
,str
, andtuple
) and some non-sequence types likedict
,file
objects, and objects of any classes you define with an__iter__()
method or with a__getitem__()
method that implements Sequence semantics.
Iterables可以在for循环中以及在许多其他地方使用
需要顺序(zip()
,map()
,…).当一个可迭代对象是
作为参数传递给内置函数iter()
时,它返回一个
对象的迭代器.此迭代器非常适合在
一组值.使用可迭代对象时,通常无需
调用iter()
或自己处理迭代器对象. for
语句会自动为您执行此操作,从而创建一个临时
未命名的变量,用于在循环持续时间内保存迭代器.
另请参见迭代器,序列和生成器.
Iterables can be used in a for loop and in many other places where a
sequence is needed (zip()
, map()
, …). When an iterable object is
passed as an argument to the built-in function iter()
, it returns an
iterator for the object. This iterator is good for one pass over the
set of values. When using iterables, it is usually not necessary to
call iter()
or deal with iterator objects yourself. The for
statement does that automatically for you, creating a temporary
unnamed variable to hold the iterator for the duration of the loop.
See also iterator, sequence, and generator.
迭代器
代表数据流的对象.反复致电 迭代器的
__next__()
方法(或将其传递给内置函数next()
)返回流中的后续项.当没有更多数据时 可用,则会引发StopIteration
异常.在这 点,迭代器对象已用尽,并且对其的任何进一步调用__next__()
方法只需再次提高StopIteration
.迭代器是 需要具有返回迭代器的__iter__()
方法 对象本身,因此每个迭代器也是可迭代的,可以在 接受其他可迭代的大多数地方.一个值得注意的例外 是尝试多次迭代遍历的代码.容器对象 (例如list
)每次传递时都会产生一个新的迭代器 到iter()
函数或在for循环中使用它.用这个尝试 迭代器将只返回与使用的相同的穷尽迭代器对象 在上一个迭代过程中,使其看起来像一个空的 容器.
An object representing a stream of data. Repeated calls to the iterator’s
__next__()
method (or passing it to the built-in functionnext()
) return successive items in the stream. When no more data are available aStopIteration
exception is raised instead. At this point, the iterator object is exhausted and any further calls to its__next__()
method just raiseStopIteration
again. Iterators are required to have an__iter__()
method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as alist
) produces a fresh new iterator each time you pass it to theiter()
function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
更多信息可以在迭代器类型中找到.
More information can be found in Iterator Types.
发电机
一个返回生成器迭代器的函数.看起来很正常 函数,但它包含用于生成a的
yield
表达式 在for
循环中可用或可以检索的一系列值 一次使用next()
功能.
A function which returns a generator iterator. It looks like a normal function except that it contains
yield
expressions for producing a series of values usable in afor
-loop or that can be retrieved one at a time with thenext()
function.
通常是指生成器功能,但可能是指生成器 在某些情况下是迭代器.如果不是预期的意思 很清楚,使用完整术语可以避免歧义.
Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.
生成器迭代器
由生成器函数创建的对象.
An object created by a generator function.
每个yield
暂时中止处理,记住位置
执行状态(包括局部变量和未决
try
-语句).生成器迭代器恢复后,它将启动
它停在哪里(与在每个地方重新开始的功能相反)
调用).
Each yield
temporarily suspends processing, remembering the location
execution state (including local variables and pending
try
-statements). When the generator iterator resumes, it picks-up
where it left-off (in contrast to functions which start fresh on every
invocation).
这篇关于迭代器vs生成器vs可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!