python中的生成器(二)


一. 剖析一下生成器对象

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

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,然后被我们捕捉到了

积一时之跬步,臻千里之遥程
原文地址:https://www.cnblogs.com/wangbaojun/p/10586625.html