http://stackoverflow.com/questions/2776829/difference-between-python-generators-vs-iterators

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

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.

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 generatorexpression) 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.

For example, a generator such as:

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

or the equivalent generator expression (genexp)

(i*i for i in xrange(start, stop))

would take more code to build as a custom iterator:

classSquares(object):def __init__(self, start, stop):
self.start = start
self.stop = stop
def __iter__(self):return self
def next(self):if self.start >= self.stop:raiseStopIteration
current = self.start * self.start
self.start +=1return current

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

def current(self):return self.start

Python Generators vs Iterators的更多相关文章

  1. Python标准模块--Iterators和Generators

    1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...

  2. (转) Python Generators(生成器)——yield关键字

    http://blog.csdn.net/scelong/article/details/6969276 生成器是这样一个函数,它记住上一次返回时在函数体中的位置.对生成器函数的第二次(或第 n 次) ...

  3. Python Generators(生成器)--yield

    参考:http://blog.csdn.net/scelong/article/details/6969276 Python生成器 什么是python生成器,意思是带有一个yield语句的函数,既然它 ...

  4. [python]Generators

    generators(生成器)是python提供的一种机制,可以让函数一边循环一边计算,通常函数是一遍执行,而生成器可以在执行中间交出变量,下次调用时从交出变量的地方重新开始,这种机制通过yield关 ...

  5. python 第五弹

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  6. python的迭代器、生成器、装饰器

    迭代器.生成器.装饰器 在这个实验里我们学习迭代器.生成器.装饰器有关知识. 知识点 迭代器 生成器 生成器表达式 闭包 装饰器 实验步骤 1. 迭代器 Python 迭代器(Iterators)对象 ...

  7. 十二. Python基础(12)--生成器

    十二. Python基础(12)--生成器 1 ● 可迭代对象(iterable) An object capable of returning its members one at a time. ...

  8. python基础---->python的使用(六)

    这里记录一下python中关于class类的一些知识.不解释就弄不懂的事,就意味着怎样解释也弄不懂. python中的类知识 一.class的属性引用与实例 class MyClass(): '''A ...

  9. Python 开发面试题

    Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...

随机推荐

  1. vim一个快速切换主题的插件(change-colorscheme,原创)

    概述 有时候我们想快速浏览主题并找到一款合适的主题,change-colorscheme将会满足我们的要求. 安装 git https://github.com/chxuan/change-color ...

  2. C# 之 静态字段初始化

          当一个字段声明中含有 static 修饰符时,由该声明引入的字段为静态字段(静态变量).当不存在 static 修饰符时,由该声明引入的字段为实例字段(实例变量).       静态字段不 ...

  3. html笔记01:顺序和无序列表

    <!DOCTYPE html> <html> <body> <li>Yellow <ul><li>Wet soil</li ...

  4. CSS skills: 6) auto hide the top bar javascript

    //jquery $(document).ready(function(){ $(window).scroll(function() { $(this).scrollTop() > 10 ? $ ...

  5. python(1) - 第一个程序 Hello World!

    进入python3的解释器环境. 我们让解释器输出 “Hello World!” 解释器成功的输出了Hello world!  程序就这样完成了. 当然上面的程序我们是在解释器中完成的. 我们可以通过 ...

  6. AVL树的插入操作(旋转)图解

    =================================================================== AVL树的概念       在说AVL树的概念之前,我们需要清楚 ...

  7. 有关ftp4j的FTPListParseException异常

    昨天换了个ftp服务器,发现程序出现了异常it.sauronsoftware.ftp4j.FTPListParseException,网上搜了下,说是FTPClient.list()时it.sauro ...

  8. mac 如何进入/usr/sbin目录

    1.进入terminal, 输入 ls /usr/sbin 2.在finder>前往文件夹,输入路径/usr/sbin

  9. poj 2524 Ubiquitous Religions(宗教信仰)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 30666   Accepted: ...

  10. asp.net 文件压缩zip下载

     今天分享下昨天做的一个东西 asp.net 的文件  zip 批量下载,首先你需要去 到http://dotnetzip.codeplex.com这个站点下载zip 的包,在里面找到 Ionic.Z ...