码迷,mamicode.com
首页 > 编程语言 > 详细

python中的生成器(二)

时间:2019-03-24 09:17:00      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:特殊   直接   subclass   exce   generator   try   pre   form   print   


一. 剖析一下生成器对象

先看一个简单的例子,我们创建一个生成器函数,然后生成一个生成器对象

def gen():
   print(start ..)
   for i in range(3):
      yield i 
   print(end...)
G=gen()
print(type(G))
>>
<type generator>

 

表示G是一个生成器对象,我们来剖析一下,里面到底有什么

 

print(dir(G))

>>[__class__, __delattr__, __doc__, __format__, __getattribute__,

 __hash__, __init__, __iter__, __name__, __new__, __reduce__, 

 __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, 

 __subclasshook__, close, gi_code, gi_frame, gi_running,

  next, send, throw]

 

 大家注意看有4个很特殊的函数close(),next(),send(),throw(),next()在python中的生成器(一)中已经介绍过了,今天我们就重点来说说send(),throw(),close()函数

 

1.send()函数

send()用于传递参数,实现与生成器的交互,当程序运行到receive=yield的时候 就会被挂起,等待生成器调用send方法,这个时候外部调用send函数,发送数据变量进来,数据变量就会传递到received

示例代码:

def echo():
   while True:
      received=yield 
      print(Receive:{}.format(received))

Echo=echo()
next(Echo)
Echo.send(First)
Echo.send(Second)
>>
Receive:First
Receive:Second

 

 

a).echo函数里面一个死循环,里面有一行关键的代码,receive=yield 这个就是send函数从外部发送过来的入口.

 

b).外部函数一开始要生成一个生成器对象也就是Echo=echo()

 

c).然后很关键的一步就是next(Echo),一定要调用一次next函数,让生成器推进到第一条yield表达式那边

 

d).接下来我们就可以把yield操作和send操作结合起来,可以获取外界所输入的数据,然后用一套流程去进行处理 

2.throw()函数

throw主要是向生成器发送一个异常,可以结束系统定义的异常(当然包括自定义的异常)

示例代码:

def gen():
   while True:
      try:
         yield First
         yield Second
      except TypeError:
         print(Catch the TypeError)
      except ValueError:
         print(Catch the ValueError)

G=gen()
print(next(G))
G.throw(ValueError)
print(next(G))
>>
First
Catch the ValueError
Second

a).创建生成器对象G

 

b),执行next(G)并打印结果,我们会得到第一个yield里缓存的结果‘First‘,并且停留在yield ‘Second‘之前

 

c).抛出一个异常类ValueError(注意ValueError是类不是字符串),当生成器收到异常直接跳过 yield ‘Second‘进入except部分,打印出‘Catch the TypeError‘

 

d).执行next(G)并打印结果,我们会回到while头部,并且消耗掉第一个yield ‘Frist‘,执行yield ‘Second‘

 

3.close()函数

close用于停止生成器,如果停止之后再调用next,就会引发StopIteration错误

示例代码:

def echo():
   while True:
      received=yield 
      print(Receive:{}.format(received))

try:
   Echo=echo()
   next(Echo)
   Echo.close()
   Echo.send(123)
except StopIteration ,e:
   print(Catch the StopIteration)
>>
Catch the StopIteration

当生成器对象Echo调用完close()之后,再调用send(‘123‘)就会抛出一个异常StopIteration,然后被我们捕捉到了

python中的生成器(二)

标签:特殊   直接   subclass   exce   generator   try   pre   form   print   

原文地址:https://www.cnblogs.com/wangbaojun/p/10586625.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!