生成器(generator),迭代器(yield)
g=(i for i in range(10)) #小括号表示生成一个迭代生成器。[]是列表生成器
g.__next__()
yield将一个函数变成生成器
import time
def f():
for i in range(10):
yield i #如果换成return i,则最终只能返回0。
print('aa')
m=f() #m是一个generator
print(m)
print(dir(m))
print(m.__next__())
print(m.__next__())
time.sleep(2)
print(m.__next__())
按字节读取文本:
def rb():
f=open('e:\\a.txt','rb')
while True:
size=6 #每次读取6个字节
block=f.read(size)
if block:
yield block
else:
break
f.close()
rr=rb()
for r in rr:
print r
yield中的next()和send():
def yed():
print "enter fun",
yield 10
yy = yed()
yy.next() #返回"enter fun"
print yy.next() #返回 enter fun 10 def yed():
print "enter fun",
yield 10
print "next1"
yield 20
yy = yed()
yy.next()
yy.next()
# #返回enter fun next1 def yed():
print "enter fun",
yield 10
print "next1"
yield 20
yy = yed()
yy.next()
yy.send('hello') #此处send的参数hello被yield 10接收了,但是并没有进行打印,所以不会被打印
#返回enter fun next1 def yed():
print "enter fun",
y1 = yield 10
print "next1",
print y1
yield 20
yy = yed()
yy.next()
yy.send('hello') #send发送的值会成为当前yield的结果,即yield 10,即y1,并且send的返回结果是下一个yield的结果(相当于了next())
#返回 enter fun next1 hello def yed():
print "enter fun",
y1 = yield 10
print "next1",
print y1
y2 = yield 20 yy = yed()
mn = yy.next() #结果为yield 10的参数,即10
ms = yy.send('hello') #结果为yield 20的参数,即20。hello被y1接收到了 print 'mn: %s, ms: %s' %(mn,ms)
#返回enter fun next1 hello
#mn: 10, ms: 20
第一次调用时,要使用next()语句或是send(None),不能使用send发送一个非None的值,否则会出错的,因为没有yield语句来接收这个值。所以第一次时next() == send(None)
send(msg) 和 next()是有返回值的,它们的返回值很特殊,返回的是下一个yield表达式的参数。比如yield 10,则返回10 。
协同处理数据:http://www.tuicool.com/articles/VBfAzaQ
我们现在利用yield关键字会自动冻结函数堆栈的特性,想象一下,假如现在有两个函数f1()和f2(),各自包含yield语句,见下例。主线程先启动f1(), 当f1()执行到yield的时候,暂时返回。这时主线程可以将执行权交给f2(),执行到f2()的yield后,可以再将执行权交给f1(),从而实现了在同一线程中交错执行f1()和f2()。f1()与f2()就是协同执行的程序,故名协程。
import random #消费生成的数据
def consume():
print 'waiting to consume'
while True:
data = yield
sum_data = sum(data)
print '%s was consumed,the sum is %d ' %(str(data),sum_data) #生成数据
def produce():
print 'begin to produce'
consumer = consume()
consumer.next()
while True:
data = random.sample(range(10),3)
print '%s produced' %(str(data))
consumer.send(data)
yield producer = produce()
for i in range(5):
producer.next() #直接执行produce()里面的代码:在produce里面先生成data,然后再将其发送给consume
可以优化为如下:
import random #消费生成的数据
def consume():
print 'waiting to consume'
while True:
data = yield
sum_data = sum(data)
print '%s was consumed,the sum is %d ' %(str(data),sum_data) #生成数据
def produce(consumer):
print 'begin to produce'
while True:
data = random.sample(range(10),3)
print '%s produced' %(str(data))
consumer.send(data)
yield consumer = consume()
producer = produce(consumer)
consumer.next() #打印waiting to consum for i in range(5):
producer.next() #直接执行produce()里面的代码:在produce里面先生成data,然后再将其发送给consume
返回结果如下:
waiting to consume
begin to produce
[2, 9, 3] produced
[2, 9, 3] was consumed,the sum is 14
[1, 5, 2] produced
[1, 5, 2] was consumed,the sum is 8
[8, 5, 4] produced
[8, 5, 4] was consumed,the sum is 17
[6, 1, 2] produced
[6, 1, 2] was consumed,the sum is 9
[8, 6, 3] produced
[8, 6, 3] was consumed,the sum is 17
异步并发????
from time import sleep,ctime def consumer(name):
print '%s want to consume' % name
while True:
bb = yield
print '%s was consumed by %s' %(bb,name) def producer(pname):
c1=consumer('A')
c2=consumer('B')
c1.next()
c2.next()
print '%s begin to produce' % pname for i in range(5):
sleep(1)
print '%s produce 2' % pname
c1.send(i)
c2.send(i) producer('zhangsan')
返回结果如下:
A want to consume
B want to consume
zhangsan begin to produce
zhangsan produce 2
0 was consumed by A
0 was consumed by B
zhangsan produce 2
1 was consumed by A
1 was consumed by B
zhangsan produce 2
2 was consumed by A
2 was consumed by B
zhangsan produce 2
3 was consumed by A
3 was consumed by B
zhangsan produce 2
4 was consumed by A
4 was consumed by B
继续看这个-2016.5.3 http://my.oschina.net/u/877348/blog/184058
生成器(generator),迭代器(yield)的更多相关文章
- Python 生成器与迭代器 yield 案例分析
前几天刚开始看 Python ,后因为项目突然到来,导致Python的学习搁置了几天.然后今天看回Python 发现 Yield 这个忽然想不起是干嘛用的了(所以,好记性不如烂笔头.).然后只能 花点 ...
- Python 生成器 (generator) & 迭代器 (iterator)
python 生成器 & 迭代器 生成器 (generator) 列表生成式 列表生成式用来生成一个列表,虽然写的是表达式,但是储存的是计算出来的结果,因此生成的列表受到内存大小的限制 示例: ...
- Python 第四篇:生成器、迭代器、装饰器、递归函数与正则表达式
一:生成器:Generator,可以理解为是一种一个函数产生一个迭代器,而迭代器里面的数据是可以通过for循环获取的,那么这个函数就是一个生成器,即生成器是有函数生成的,创建生成器使用()表示,比如g ...
- Python(迭代、三元表达式、列表生成、生成器、迭代器)
迭代 什么是迭代 1 重复 2 下次重复一定是基于上一次的结果而来 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). ...
- 10、Python迭代器与生成器(iterator、for循环、generator、yield)
一.迭代器(foreach) 1.可迭代的对象 内置有__iter__方法的都叫可迭代的对象. Python内置str.list.tuple.dict.set.file都是可迭代对象. x = 1._ ...
- ES6中的迭代器(Iterator)和生成器(Generator)
前面的话 用循环语句迭代数据时,必须要初始化一个变量来记录每一次迭代在数据集合中的位置,而在许多编程语言中,已经开始通过程序化的方式用迭代器对象返回迭代过程中集合的每一个元素 迭代器的使用可以极大地简 ...
- Python进阶内容(四)--- 迭代器(Iterator)与生成器(Generator)
迭代器 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的ge ...
- python生成器(generator)、迭代器(iterator)、可迭代对象(iterable)区别
三者联系 迭代器(iterator)是一个更抽象的概念,任何对象,如果它的类有next方法(next python3)和__iter__方法返回自己本身,即为迭代器 通常生成器是通过调用一个或多个yi ...
- Python之生成器(generator)和迭代器(Iterator)
generator 生成器generator:一边循环一边计算的机制. 生成器是一个特殊的程序,可以被用于控制循环的迭代行为.python中的生成器是迭代器的一种,使用yield返回值函数,每次调用y ...
- TypeScript 迭代器(iterator)和生成器(generator)
⒈迭代器(iterator) 1.可迭代性 当一个对象实现了Symbol.iterator属性时,我们认为它是可迭代的. 一些内置的类型如 Array,Map,Set,String,Int32Arra ...
随机推荐
- glog功能介绍
1. 概述 Google glog是一个基于程序级记录日志信息的c++库,编程使用方式与c++的stream操作类似,例: LOG(INFO) << &quo ...
- C#的进度条透明的情况(穿透问题)
如图: 很明显,可以看到透过进度条的框框可以看到下面的软件界面, 之前出现这种情况是因为pictureBox和panel层级之间的关系造成的, 没想到这次的原因居然不是这样,而是因为少了backCol ...
- 移动设备的HTML页面中图片实现滚动加载
如今移动互联网风靡全球,移动页面的元素也是丰富多彩,一个移动页面的图片超过10张已经是再正常不过的事情了.但是相对,很多移动用户还停留在2G,3G这样的网络中.那么这样带宽的用户,在浏览这样的页面时, ...
- Java数据库连接池原理与简易实现
1.什么是数据库连接池 我们现在在开发中一定都会用到数据库,为了提高我们的系统的访问速度,数据库优化是一个有效的途径.我们现在开发中使用数据库一般都要经历以下的四个步骤:(1)加载数据库的驱动类,(2 ...
- 使用gitlab, jenkins搭建CI(持续集成)系统(1) -- 准备环境
1. 环境设计 搭建一个从开发到测试知道发布上线可以自动换完成的CI系统.这个系统中包含4个环境. 开发(dev)环境: 码农使用. 测试(test)环境: 测试人员使用. 预发布(prepublis ...
- 默 of 2018:年终总结
目录 1 概述:在平凡中求变 2 专业分流:一个时代的终点,我的新起点 2.1 我在专业分流前夕的境况 2.2 专业分流情况概述,以及对一篇文章的回顾 2.3 总结与余绪 2.4 附:关于理科与工科的 ...
- 让Div居中的方法
1,设置元素宽度和margin值 div{ width : 90%; margin : 0px auto } 2, 定位元素,并设置其左右距离,但,不能设定宽度 div{ position : abs ...
- BIO、NIO和AIO的区别
一:事件分离器 在IO读写时,把 IO请求 与 读写操作 分离调配进行,需要用到事件分离器.根据处理机制的不同,事件分离器又分为:同步的Reactor和异步的Proactor. Reactor模型: ...
- C# 值类型 引用类型
CLR 定义了两种类型,ReferenceTypes引用类型 和 ValueTypes 值类型.我们定义的各种Class都是引用类型,而我们用的decimal int 之类是值类型. 他们有什么区别呢 ...
- 浅谈JVM-图解类加载机制
一.目录 二.类加载机制流程 1.什么是类加载机制? JVM把class文件加载到内存里面,并对数据进行校验.准备.解析和初始化,最终能够被形成被JVM可以直接使用的Java类型的过程. 2.类加载流 ...