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 ...
随机推荐
- RTB广告展示分步说明
原文:http://contest.ipinyou.com/cn/manual.shtml RTB (Real Time Bidding, 实时竞价) 是近年来计算广告领域最激动人心的进展之一. 它增 ...
- 电商平台如何接入快递鸟电子面单API?
快递鸟是全球物流接口服务商,为电商 ERP.电商平台.仓储.清关公司提供物流跟踪.电子面单.智选物流.物流金融.在线下单等服务,解决电商的物流管理模块和金融模块.现就对快递鸟电子面单API做基本描述, ...
- Code the Tree
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2292 Accepted: 878 Description A tree ...
- [转]永久告别Android的背景选择器Selector!无需切很多图了!
package com.zoke.custom.autobg; import android.content.Context; import android.content.res.TypedArra ...
- 关于VMware桥接的注意事项
VMware 使用桥接 想固定住虚拟机的IP的同时还可以访问外网. 通过Linux的可视化操作界面固定设置IP,网关,子网掩码等配置信息,如下图: 附录本地Windows中的IP地址信息: 虚拟机和 ...
- Sublime text3 安装
Sublime是一款跨平台的前端开发神器,国外的一款共享软件,虽然是未注册的但不影响使用. 一.下载最新版的安装包 官网地址:http://www.sublimetext.com/3 --portab ...
- VS的启动方式
启动VS的两种方式1.双击图标2.调出cmd,输入 devenv
- px和em之间的转换
很多网页设计者在写css时都是在通用选择器中就设置了字体的大小,中文情况下一般为12px.然而IE浏览器却无法调整那些使用px作为单位的字体大小.其实使用em作为单位是可以避免这一情况的. 一.em和 ...
- Ehcache(2.9.x) - API Developer Guide, Cache Exception Handlers
About Exception Handlers By default, most cache operations will propagate a runtime CacheException o ...
- css尖角
.market-nav-arrow { ; ; ; border-style: solid; border-width: 7px 0px 7px 7px; border-color: transpar ...