Python Generators vs Iterators
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的更多相关文章
- Python标准模块--Iterators和Generators
1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...
- (转) Python Generators(生成器)——yield关键字
http://blog.csdn.net/scelong/article/details/6969276 生成器是这样一个函数,它记住上一次返回时在函数体中的位置.对生成器函数的第二次(或第 n 次) ...
- Python Generators(生成器)--yield
参考:http://blog.csdn.net/scelong/article/details/6969276 Python生成器 什么是python生成器,意思是带有一个yield语句的函数,既然它 ...
- [python]Generators
generators(生成器)是python提供的一种机制,可以让函数一边循环一边计算,通常函数是一遍执行,而生成器可以在执行中间交出变量,下次调用时从交出变量的地方重新开始,这种机制通过yield关 ...
- python 第五弹
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...
- python的迭代器、生成器、装饰器
迭代器.生成器.装饰器 在这个实验里我们学习迭代器.生成器.装饰器有关知识. 知识点 迭代器 生成器 生成器表达式 闭包 装饰器 实验步骤 1. 迭代器 Python 迭代器(Iterators)对象 ...
- 十二. Python基础(12)--生成器
十二. Python基础(12)--生成器 1 ● 可迭代对象(iterable) An object capable of returning its members one at a time. ...
- python基础---->python的使用(六)
这里记录一下python中关于class类的一些知识.不解释就弄不懂的事,就意味着怎样解释也弄不懂. python中的类知识 一.class的属性引用与实例 class MyClass(): '''A ...
- Python 开发面试题
Python部分 将一个字符串逆序,不能使用反转函数 求从10到100中能被3或5整除的数的和 What is Python? What are the benefits of using Pytho ...
随机推荐
- 使用Loadrunner进行接口测试
在工作中很多时候都需要进行接口测试,如果只是进行接口的功能测试这个很简单,两种类型: 1.如果是基于get的直接使用浏览器进行访问,查看服务器返回的数据是否正确就行, 2.如果是基于post的可以接触 ...
- android复合控件
一.复合控件TopBar 创建复合控件可以很好地创建出具有重用功能的控件集合.比如TopBar. 做法:一般需要继承ViewGroup,再给它添加指定功能的控件. 以TopBar为例: 1 ...
- sql 自定义函数-16进制转10进制
做过笔记,好记性不如烂笔头: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[HEXTOINT]') and ...
- Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary
NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map. 通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以 ...
- yii2在ubuntu下执行定时任务
一.编辑yii console/controllers TestController.php 二./usr/ 包括与系统用户直接有关的文件和目录创建sh_scripts目录,/usr/sh_scrip ...
- (转)嵌入式学习准备---linux c 文件锁
(1)fcntl函数说明 前面的这5个基本函数实现了文件的打开.读写等基本操作,这一节将讨论的是,在文 件已经共享的情况下如何操作,也就是当多个用户共同使用.操作一个文件的情况,这时,Linux 通常 ...
- LeetCode 260
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- IOS微信中看文章跳转页面后点击返回无效
经过查找原因发现,下面两种链接,链接1返回不了,链接2可以返回. 链接1:http://mp.weixin.qq.com/s?__biz=MzA5NDY5MzcyNA==&mid=265089 ...
- centos 安装ss-QT5
方法一(DNF指令): 1.如果未安装DNF,请跳转至:http://dev.fjuts.com:83/blog/index.PHP/Linux/261.html2.添加shadowsocks 的 c ...
- Font Awesome图标字体库(2015年05月25日)
Font Awesome是一款非常棒的字体图标工具,给个地址,具体的自已慢慢去体会,只能帮你到这儿了...... http://fortawesome.github.io/Font-Awesome/ ...