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. 今天再分享一个TextView内容风格化的类

    /* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...

  2. C++_快速排序

    void quick_sort(int s[],int l,int r) { if(l<r) { int i=l,j=r,x=s[l]; while(i<j) { while( i< ...

  3. Entity Framework实体框架使用TrackerEnabledDbContext进行操作日志跟踪

    在EF实体框架中进行日志跟踪,一般都是自己写个Log实体类,在数据保存时进行属性原始值验证来进行日志跟踪.当然还可以使用一些第三扩展库例如:entity framework extended进行日志记 ...

  4. Android(java)学习笔记93:Android布局详解之一:FrameLayout

    FrameLayout是最简单的布局了.所有放在布局里的控件,都按照层次堆叠在屏幕的左上角.后加进来的控件覆盖前面的控件. 在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义.控件 ...

  5. 使用addClass()设置自增类名

    今天在翻阅JQuery API的时候发现,addClass()方法不仅可以添加类,同时可以调用回调函数. 代码如下: <body> <p>item1</p> < ...

  6. 关于在Eclipse中构建patch开发环境

    git仓库中不能添加空文件夹. 至于为什么git 仓库中不能添加空文件夹,我没有找到具体的原因. 在V2EX上有人说: 我猜是因为git的内部实现,即只对文件做快照决定的吧,另一方面空文件夹确实对源代 ...

  7. win8.1恢复win7 CTRL+Space切换输入法

    win8用起来还是有很多好用的东西的,但是最让我受不了的就是输入法的切换,可以说是丧心病狂!!!折磨了我好久,今天终于找到了解决的办法! 那就是这位博客园的哥们给出的方案! http://www.cn ...

  8. linux nginx启动 重启 关闭命令

    启动操作 nginx -c /usr/local/nginx/conf/nginx.conf -c参数指定了要加载的nginx配置文件路径 停止操作停止操作是通过向nginx进程发送信号来进行的 步骤 ...

  9. getComputedStyle和currentStyle

    /*alert(div.style.width)*/ //null function getstyle(obj,name){ if(obj.currentStyle) { return obj.cur ...

  10. CXF(2.7.10) - A simple JAX-WS service

    1. 下载 apache-cxf-x.x.x.zip,在工程导入依赖的 jar 包.也可以基于 Maven 构建工程. 2. 定义服务接口. package com.huey.demo.ws; imp ...