python随用随学20200221-生成器中的send(),throw()和close()方法
send()方法
文档定义
generator.send(value)
Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value.
- 这里要注意send方法和next方法的区别,在于send方法会首先把上一次挂起的yield语句的返回值通过参数设定,从而实现和生成器方法的交互.
send(None)和next()方法完全等价.
代码示例
- def gen():
- x = yield 1
- print('gen---{0}'.format(x))
- y = yield 2
- print('gen---{0}'.format(y))
- z = yield 3
- print('gen---{0}'.format(z))
- g = gen()
- result0 = g.send(None)
- print('main---{0}'.format(result0))
- result1 = g.send(111)
- print('main---{0}'.format(result1))
- result2 = g.send(222)
- print('main---{0}'.format(result2))
- result3 = g.send(333)
- print('main---{0}'.format(result3))
把上面那一段读懂之后 这个结果就好理解了.

- 首先
result0 = g.send(None)执行,激活生成器到yeild 1. 生成器返回1 输出main---1 - 第二步到
result1 = g.send(111)从上次挂起的地方向生成器传递111的值,也就是x=111接着往下执行输出gen---111 - 往下执行到yeild 2,返回2 然后打印main---2,同理下一步到
result2 = g.send(222) - y得到值222,输出
gen---222 - 然后继续执行到yeild 3,返回3,输出
main---3 - 再下一步send(333)的时候,从上次终端的位置
z=yield 3获取输入值,z被赋值为333,打印gen---333 - 往下没有yield了,抛出StopIteration的异常.
刚开始没搞明白是因为没有搞明白这个send进去的值在那里接收. 这里注意的是send进去的值在上一次yeild挂起的位置接收.
throw()方法
文档定义
generator.throw(type[, value[, traceback]])
Raises an exception of type type at the point where the generator was paused, and returns the next value yielded by the generator function. If the generator exits without yielding another value, a StopIteration exception is raised. If the generator function does not catch the passed-in exception, or raises a different exception, then that exception propagates to the caller.
注意!!! 在上次yeild挂起的地方传入exception!!!
代码示例1-异常在生成器中被捕获
- def gen():
- n=0
- while True:
- try:
- yield n
- n +=1
- except OverflowError:
- print("got it!")
- g = gen()
- result1 = next(g)
- print(result1)
- result2 = g.throw(OverflowError)
- print(result2)
- result3 = next(g)
- print(result3)

结果比较简单,我们来看一下:
result1 = next(g)没毛病 n=0进去的,然和yield出来的也是0result2=g.throw(OverflowError)在上次挂起的地方,传入这个异常. 然后异常被捕获,n还是0 因为下面的n+=n跟本没执行.- 下面就没啥好解释的了
代码示例2-异常在生成器中没有被捕获
- import sys
- def gen():
- n = 0
- while True:
- yield n
- n+=1
- g = gen()
- result1 = next(g)
- print(result1)
- try:
- result2 = g.throw(NameError)
- except NameError:
- print('Main function catch the exception')
- print(sys.exc_info())
- try:
- print(result2)
- except NameError:
- print('cathe NameError')
- print(sys.exc_info())
- print(next(g))
看着例子
- throw的异常在生成器内部没有捕获的话,会直接传递到调用生成器的函数中去.
- 这里result2因为赋值还没有发生就抛出了异常,所以result2是不存在的.
- 在异常抛出后,生成器终止,不能再继续调用.

代码示例3
- def gen():
- try:
- # 注意是在当前暂停的 yield 处抛出异常
- # 所以要在这里捕获
- yield 1
- except Exception as e:
- print('在生成器内部捕获了异常')
- print(e.args)
- print('处理完毕,假装什么也没发生')
- # yield 2
- g = gen()
- print(next(g))
- result = g.throw(TypeError,'类型错误~~')
- print(result)
print(next(g))这里没问题,正常返回1- 下一步
result = g.throw(TypeError,'类型错误~~')把异常整进去之后,记得!是从上一次yeild挂起的地方!!! 直接被生成器中的except捕获了. - 之后因为下面没有yeild的值了,所以会报一个StopIteration的异常回来.

close()方法
文档定义
generator.close()
Raises a GeneratorExit at the point where the generator function was paused. If the generator function then exits gracefully, is already closed, or raises GeneratorExit (by not catching the exception), close returns to its caller. If the generator yields a value, a RuntimeError is raised. If the generator raises any other exception, it is propagated to the caller. close() does nothing if the generator has already exited due to an exception or normal exit.
文档解读:
- 在生成器暂停的地方会返回
GeneratorExit
代码示例1-不在生成器内捕获GeneratorExit异常
- def gen():
- print("1 will be yielded")
- yield 1
- print('2 will be yielded')
- yield 2
- g = gen()
- print(next(g))
- g.close()
- print(next(g))

- close方法正常返回,且生成器内的GeneratorExit不传递给调用方
- close之后生成器注销,所以在此next的时候会报StopIteration异常
代码示例2
- def gen():
- print("1 will be yielded")
- try:
- yield 1
- except GeneratorExit:
- print('get generator exit exception')
- print("generator finished")
- g = gen()
- print(next(g))
- g.close()
生成器退出的时候抛出StopIteration异常,但是这个异常不会传递给调用方.
代码示例3--GeneratorExit之后还有yield语句
- def gen():
- try:
- yield 1
- except GeneratorExit:
- print('捕获到 GeneratorExit')
- print('尝试在 GeneratorExit 产生后 yield 一个值')
- yield 2
- print('生成器结束')
- g = gen()
- next(g)
- g.close()

- 在GeneratorExit抛出之后还有yield语句的话 有可能会产生RuntimeError.
- 所以不要在except和finally中写yield,因为不知道啥时候就会出问题.
- 另外,在生成器被垃圾回收的时候,也会自动调用close方法
最后,对于一个已经关闭的生成器对象,close方法不会有任何的作用. 不执行任何操作.
换句话说,只有激活的生成器才会触发GeneratorExit异常.
终于特么写完了...累死我了...
本文参考了 CSDN(团子大元帅)的文章
python随用随学20200221-生成器中的send(),throw()和close()方法的更多相关文章
- python特性(八):生成器对象的send方法
生成器对象是一个迭代器.但是它比迭代器对象多了一些方法,它们包括send方法,throw方法和close方法.这些方法,主要是用于外部与生成器对象的交互.本文先介绍send方法. send方法有一个参 ...
- Python编程系列---获取请求报文行中的URL的几种方法总结
在浏览器访问web服务器的时候,服务器收到的是一个请求报文,大概GET请求的格式大概如下: 先随便拿到一个请求报文,蓝色即为我们要获取的 GET /index.html HTTP/1.1 Hos ...
- [Python] 文科生零基础学编程系列二——数据类型、变量、常量的基础概念
上一篇:[Python] 文科生零基础学编程系列--对象.集合.属性.方法的基本定义 下一篇: (仍先以最简单的Excel的VBA为例,语法与Python不同,但概念和逻辑需要理解透彻) p.p1 { ...
- python生成器中yield和send分析
生成器 在python中生成器是指用代码实现迭代器的的功能本质还是迭代器,只不过是代码实现迭代器功能.在python中生成器是由函数实现的,通常我们在函数中加入yeild就可以实现生成器. 生成器中y ...
- python极简教程05:生成器和匿名函数
测试奇谭,BUG不见. 讲解之前,我先说说我的教程和网上其他教程的区别: 1 我分享的是我在工作中高频使用的场景,是精华内容: 2 我分享的是学习方法,亦或说,是指明你该学哪些.该重点掌握哪些内容: ...
- python基础知识7——迭代器,生成器,装饰器
迭代器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器 ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Python 迭代器之列表解析与生成器
 [TOC] 1. 列表解析 1.1 列表解析基础 列表解析把任意一个表达式应用到一个迭代对象中的元素 Python内置ord函数会返回一个字符的ASCII整数编码(chr函数是它的逆过程, 它将A ...
- Python中的str与unicode处理方法
Python中的str与unicode处理方法 2015/03/25 · 基础知识 · 3 评论· Python 分享到:42 原文出处: liuaiqi627 的博客 python2.x中处理 ...
随机推荐
- 创建dynamics CRM client-side (十四) - Web API
Xrm.WebApi 是我们做前端开发不可不缺少的内容. Xrm.WebApi 分为online和offline online: 可以实现和服务器的CRUD交互 offline: 多用于mobile ...
- HTML-02-常用标签演示
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java小白入门:聊聊Java这门编程语言
一.什么叫做编程 首先我们应该了解一下什么叫做编程. 百度百科词条的解释: 编程是编定程序的简称,是让计算机代我们解决某个问题,是对某个计算体系规定一定的运算方式,使计算体系按照该计算方式运行,并最终 ...
- [C语言学习笔记二] extern 函数的用法
extern 用来定义一个或多个变量.其后跟数据类型名和初始值.例如: extern int a =10 它与 int,long long int,double,char的本质区别,在于 extern ...
- React报错Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`
引言 最近在忙毕业设计,博客也很久没更新了,毕业设计使用vue做了一个校园寻物网站,现在开始学Raect,记录一下自己遇到问题,react-redux的connect方法使得组件与Redux建立了联系 ...
- 1-NoSQL介绍及Redis安装
背景 随着互联网的不断发展和软件架构的不断复杂化,同时随着网站访问量的日渐上升,导致传统单机关系型数据库应用已经无法满足人们的需求,在高并发的场景下,频繁的数据库存取操作使得服务器压力剧增,甚至导致服 ...
- 10、VLAN
VLAN综述:1.分段2.灵活性3.安全性 一个VLAN=一个广播域=逻辑网段(子网) VLAN运作:1.每个逻辑的VLAN就象一个独立的物理桥2.交换机上的每一个端口都可以分配给不同的VLAN3.默 ...
- 项目架构级别规约框架Archunit调研
背景 最近在做一个新项目的时候引入了一个架构方面的需求,就是需要检查项目的编码规范.模块分类规范.类依赖规范等,刚好接触到,正好做个调研. 很多时候,我们会制定项目的规范,例如: 硬性规定项目包结构中 ...
- CCF_201503-1_图像旋转
水. #include<iostream> #include<cstdio> using namespace std; ][]; int main() { int n,m; c ...
- 1 使用MySQL
1.1 连接 主机名(localhost) 端口(3306) 一个合法的用户名 用户口令 1.2 选择数据库 USE crashcourse 1.3 了解数据库和表 SHOW databases; s ...