Python 的生成器和迭代器的区别

2022-01-10 00:00:00 python generator iterator

问题描述

迭代器和生成器有什么区别?关于何时使用每种情况的一些示例会很有帮助.

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.


解决方案

iterator 是一个更笼统的概念:任何类具有 __next__ 方法(Python 2 中的 next) 和一个 __iter__ 方法,该方法执行 return self.

iterator is a more general concept: any object whose class has a __next__ method (next in Python 2) and an __iter__ method that does return self.

每个生成器都是一个迭代器,但反之则不然.生成器是通过调用具有一个或多个 yield 表达式(yield 语句,在 Python 2.5 及更早版本中)的函数来构建的,并且是满足上一段定义的对象迭代器.

Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions (yield statements, in Python 2.5 and earlier), and is an object that meets the previous paragraph's definition of an iterator.

当您需要一个具有某种复杂的状态维护行为的类,或者想要公开除 __next__ (和 __iter____init__).大多数情况下,一个生成器(有时,对于足够简单的需求,一个生成器表达式)就足够了,而且它更容易编码,因为状态维护(在合理的范围内)基本上是为你完成"的.由框架暂停和恢复.

You may want to use a custom iterator, rather than a generator, when you need a class with somewhat complex state-maintaining behavior, or want to expose other methods besides __next__ (and __iter__ and __init__). Most often, a generator (sometimes, for sufficiently simple needs, a generator expression) is sufficient, and it's simpler to code because state maintenance (within reasonable limits) is basically "done for you" by the frame getting suspended and resumed.

例如生成器如:

def squares(start, stop):
    for i in range(start, stop):
        yield i * i

generator = squares(a, b)

或等效的生成器表达式(genexp)

or the equivalent generator expression (genexp)

generator = (i*i for i in range(a, b))

需要更多代码来构建自定义迭代器:

would take more code to build as a custom iterator:

class Squares(object):
    def __init__(self, start, stop):
       self.start = start
       self.stop = stop
    def __iter__(self): return self
    def __next__(self): # next in Python 2
       if self.start >= self.stop:
           raise StopIteration
       current = self.start * self.start
       self.start += 1
       return current

iterator = Squares(a, b)

但是,当然,使用类 Squares 您可以轻松地提供额外的方法,即

But, of course, with class Squares you could easily offer extra methods, i.e.

    def current(self):
       return self.start

如果您对应用程序中的此类额外功能有任何实际需求.

if you have any actual need for such extra functionality in your application.

相关文章