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()方法完全等价.

代码示例

  1. def gen(): 
  2. x = yield 1 
  3. print('gen---{0}'.format(x)) 
  4. y = yield 2 
  5. print('gen---{0}'.format(y)) 
  6. z = yield 3 
  7. print('gen---{0}'.format(z)) 
  8.  
  9. g = gen() 
  10.  
  11. result0 = g.send(None) 
  12. print('main---{0}'.format(result0)) 
  13. result1 = g.send(111) 
  14. print('main---{0}'.format(result1)) 
  15. result2 = g.send(222) 
  16. print('main---{0}'.format(result2)) 
  17. result3 = g.send(333) 
  18. print('main---{0}'.format(result3)) 
  19.  

把上面那一段读懂之后 这个结果就好理解了.


pic-1582723999405.png

  • 首先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-异常在生成器中被捕获

  1. def gen(): 
  2. n=0 
  3. while True: 
  4. try: 
  5. yield n 
  6. n +=1 
  7. except OverflowError: 
  8. print("got it!") 
  9.  
  10. g = gen() 
  11.  
  12. result1 = next(g) 
  13. print(result1) 
  14.  
  15. result2 = g.throw(OverflowError) 
  16. print(result2) 
  17.  
  18. result3 = next(g) 
  19. print(result3) 
  20.  


pic-1582723999406.png

结果比较简单,我们来看一下:

  • result1 = next(g) 没毛病 n=0进去的,然和yield出来的也是0
  • result2=g.throw(OverflowError) 在上次挂起的地方,传入这个异常. 然后异常被捕获,n还是0 因为下面的n+=n跟本没执行.
  • 下面就没啥好解释的了

代码示例2-异常在生成器中没有被捕获

  1. import sys 
  2. def gen(): 
  3. n = 0 
  4. while True: 
  5. yield n 
  6. n+=1 
  7.  
  8. g = gen() 
  9.  
  10. result1 = next(g) 
  11. print(result1) 
  12.  
  13. try: 
  14. result2 = g.throw(NameError) 
  15. except NameError: 
  16. print('Main function catch the exception') 
  17. print(sys.exc_info()) 
  18.  
  19. try: 
  20. print(result2) 
  21. except NameError: 
  22. print('cathe NameError') 
  23. print(sys.exc_info()) 
  24.  
  25. print(next(g)) 
  26.  

看着例子

  • throw的异常在生成器内部没有捕获的话,会直接传递到调用生成器的函数中去.
  • 这里result2因为赋值还没有发生就抛出了异常,所以result2是不存在的.
  • 在异常抛出后,生成器终止,不能再继续调用.


pic-1582723999406.png

代码示例3

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


pic-1582723999406.png

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异常

  1. def gen(): 
  2. print("1 will be yielded") 
  3. yield 1 
  4. print('2 will be yielded') 
  5. yield 2 
  6.  
  7. g = gen() 
  8.  
  9. print(next(g)) 
  10. g.close() 
  11. print(next(g)) 
  12.  


pic-1582723999406.png

  • close方法正常返回,且生成器内的GeneratorExit不传递给调用方
  • close之后生成器注销,所以在此next的时候会报StopIteration异常

代码示例2

  1. def gen(): 
  2. print("1 will be yielded") 
  3. try: 
  4. yield 1 
  5. except GeneratorExit: 
  6. print('get generator exit exception') 
  7. print("generator finished") 
  8.  
  9. g = gen() 
  10.  
  11. print(next(g)) 
  12. g.close() 
  13.  

生成器退出的时候抛出StopIteration异常,但是这个异常不会传递给调用方.

代码示例3--GeneratorExit之后还有yield语句

  1. def gen(): 
  2. try: 
  3. yield 1 
  4. except GeneratorExit: 
  5. print('捕获到 GeneratorExit') 
  6. print('尝试在 GeneratorExit 产生后 yield 一个值') 
  7. yield 2 
  8.  
  9. print('生成器结束') 
  10.  
  11. g = gen() 
  12. next(g) 
  13. g.close() 
  14.  


pic-1582723999407.png

  • 在GeneratorExit抛出之后还有yield语句的话 有可能会产生RuntimeError.
  • 所以不要在except和finally中写yield,因为不知道啥时候就会出问题.
  • 另外,在生成器被垃圾回收的时候,也会自动调用close方法

最后,对于一个已经关闭的生成器对象,close方法不会有任何的作用. 不执行任何操作.
换句话说,只有激活的生成器才会触发GeneratorExit异常.

终于特么写完了...累死我了...

本文参考了 CSDN(团子大元帅)的文章

Python 生成器与它的 send,throw,close 方法

原文地址:https://www.cnblogs.com/thecatcher/p/12369210.html