迭代器和生成器有什么区别?关于何时使用每种情况的一些示例会很有帮助.
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.
这篇关于Python 的生成器和迭代器的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在python中的感兴趣区域周围绘制一个矩形How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
如何使用 OpenCV 检测和跟踪人员?How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
如何在图像的多个矩形边界框中应用阈值?How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
如何下载 Coco Dataset 的特定部分?How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
根据文本方向检测图像方向角度Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
使用 Opencv 检测图像中矩形的中心和角度Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)