小谈 python 生成器

生成器:  迭代器的另一种操作,它能干的事,list也都能干,只不过在需要处理的序列比较大时,列表解析比较费内存。
生成器表达式: 通列表解析语法,只不过把列表解析的[]换成()


例如:
>>> gen = (x**2 for x in range(5))
>>> gen
<generator object <genexpr> at 0x0000000002FB7B40>
>>> for g in gen:
...   print(g, end='-')
...
0-1-4-9-16-
>>> for x in [0,1,2,3,4,5]:
...   print(x, end='-')
...
0-1-2-3-4-5-

生成器函数:  在函数中如果出现了yield 关键字,称这个函数为生成器函数
def odd():
n=1
while True:
yield n
n+=2
odd_num = odd()
count = 0
for o in odd_num:
if count >=5: break
print(o)
count +=1
 
生成器是包含有__iter()和next__()方法的,所以可以直接使用for来迭代,迭代到最后会返回StopIteration前提是没return  当然了 return 也不会返回值
其实相当于分解一个迭代器,分解的条件是 yield 直到被 return 或者 StopIteration 当谈论 如果在return后返回一个值,
那么这个值为StopIteration异常的说明,不是程序的返回值。 生成器是没有办法返回值的。
 如
 def test():
    yield 'a'
    yield 'b'
    return 'd'
    yield 'abc'


tt = test()
print(next(tt))  这是定义大a
print(next(tt))  这是定义到b  后面有return 就不走 




生成器的一些方法:
close()  主动关闭生成器,后面的next 都是 StopIteration  通过 调用生成器返回的对象.close() 来实现
send() 接受外部的变量传入   这是生成器函数最难理解的地方,也是最重要的地方,实现后面的协程就全靠它了
如:
def gen():
value=0
while True:
receive=yield value
print('参数值 =  ',receive)
if receive=='e':
break
value = 'got: %s' % receive


g=gen()
print(g.send(None))
print(g.send('aaa'))
print(g.send(3))
print(g.send('e'))


通过g.send(None)或者next(g)可以启动生成器函数,并执行到第一个yield语句结束的位置。
此时,执行完了yield语句,但是没有给receive赋值。
yield value会输出初始值0
注意:在启动生成器函数时只能send(None),如果试图输入其它的值都会得到错误提示信息。
通过g.send('aaa'),会传入aaa,并赋值给receive,然后计算出value的值,并回到while头部,执行yield value语句有停止。
此时yield value会输出"got: aaa",然后挂起。
通过g.send(3),会重复第2步,最后输出结果为"got: 3"
当我们g.send('e')时,程序会执行break然后推出循环,最后整个函数执行完毕,所以会得到StopIteration异常。
最后的执行结果如下:


0
got: aaa
got: 3
Traceback (most recent call last):
File "h.py", line 14, in <module>
 print(g.send('e'))
StopIteration






出自:http://www.cnblogs.com/jessonluo/p/4732565.html





猜你喜欢

转载自blog.csdn.net/guoxinjie17/article/details/77874628
今日推荐