python iterable与iterator,generator,generator expression

iterable

An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an iter() or getitem() method. 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.
翻译:
iterable(可迭代对象)是能够一次性返回其成员的对象。 常见iterable包括所有序列类型(如list,str和tuple)以及一些非序列类型,如dict和文件,以及任何定义了iter ()方法或getitem ()的类的对象。 Iterables可以用于for循环中,也可以用于许多需要序列的地方(zip(),map(),…))。 当一个iterable(可迭代对象)作为参数传递给内置函数iter()时,它返回该对象的迭代器iterator。 当使用iterables时,通常不需要显式调用iter()或者自己处理iterator对象。 for语句为您自动执行,创建一个临时的未命名变量来保存该iterable(可迭代对象)的迭代器循iterator。 另请参见迭代器iterator序列sequence生成器generator

iterator

An object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration 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 a list) produces a fresh new iterator each time you pass it to the iter() 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.

翻译
迭代器iterator是表示数据流的对象。 对迭代器的next()方法的重复调用会依次返回数据流中的连续项。 当没有更多数据可用即到达数据流的末尾时,会引发StopIteration异常。 当到达数据流的末尾时,迭代器iterator就被耗尽了,对其next()方法的任何进一步调用只会再次引发StopIteration异常。 迭代器需要具有返回迭代器对象本身的iter ()方法,因此每个迭代器也是可迭代的(即每个iterator也是一个iterable),并且可以在大多数接受iterable的地方可以使用iterator。 一个值得注意的例外是尝试多次迭代传递的代码。 容器对象(如列表)每次将其传递给iter()函数或在for循环中使用时,都会生成一个新的新迭代器。 尝试使用迭代器将只返回与前一次迭代通过相同的耗尽的迭代器对象,使其看起来像一个空容器。

更多信息可以在Iterator类型中找到。

generator

A function which returns an iterator. It looks like a normal function except that it contains yield statements for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function. Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator resumes, it picks-up where it left-off (in contrast to functions which start fresh on every invocation).

generator expression

An expression that returns an iterator. It looks like a normal expression followed by a for expression defining a loop variable, range, and an optional if expression. The combined expression generates values for an enclosing function:

sum(i*i for i in range(10)) # sum of squares 0, 1, 4, … 81
285

显然,iterable,generator,generator expression都和iterator有关联。

发现了一些比较好的文章:
http://www.cnblogs.com/woshitianma/p/3217403.html
http://python.jobbole.com/87832/
http://python.jobbole.com/87296/?utm_source=blog.jobbole.com&utm_medium=relatedPosts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值