协程关键字 yield 和 yield from

2018-09-14  本文已影响0人  DamaoShao

yield

当一个函数中出现yield关键字的时候,那么这个函数就是一个生成器。可以用for循环或者next()函数来迭代。

In [41]: def foo():
    ...:     for i in range(5):
    ...:         yield i
    ...:

In [42]: foo()
Out[42]: <generator object foo at 0x1027de620>

In [43]: f=foo()

In [44]: next(f)
Out[44]: 0

In [45]: next(f)
Out[45]: 1

In [46]: next(f)
Out[46]: 2

In [47]: next(f)
Out[47]: 3

In [48]: next(f)
Out[48]: 4

In [49]: next(f)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-49-aff1dd02a623> in <module>()
----> 1 next(f)

StopIteration:

yield 可以接收到外界传入的变量。

In [12]: def foo():
    ...:     msg=None
    ...:     while True:
            # 注意这里
            # 首先向外抛出msg
            # 暂停,等待next/send
            # 接收赋值
    ...:         value = yield msg
    ...:         if value == 'exit':
    ...:             break
    ...:         if value is not None:
    ...:             msg = 'receive value: '+ str(value)
    ...:         else:
    ...:             msg = 'receive value: None'
    ...:

In [13]: f=foo()

# 第一个next是启动
In [14]: next(f)

# 下面可以看出next(f)其实就是send(None)
In [15]: next(f)
Out[15]: 'receive value: None'

In [16]: next(f)
Out[16]: 'receive value: None'

In [17]: next(f)
Out[17]: 'receive value: None'

In [18]: f.send(1)
Out[18]: 'receive value: 1'

In [19]: f.send('damao')
Out[19]: 'receive value: damao'

In [20]: f.send('hello world')
Out[20]: 'receive value: hello world'

In [21]: f.send('exit')
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-21-90c57e705761> in <module>()
----> 1 f.send('exit')

StopIteration:

yield from

先看代码

In [22]: def foo1():
    ...:     yield range(5)
    ...:

In [23]: def foo2():
    ...:     yield from range(5)
    ...:

In [24]: def foo3():
    ...:     for i in range(5):
    ...:         yield i
    ...:

In [25]: foo1()
Out[25]: <generator object foo1 at 0x1112759e8>

# 直接返回了一个可迭代对象
In [26]: for i in foo1():
    ...:     print(i)
    ...:
range(0, 5)

In [27]: foo2()
Out[27]: <generator object foo2 at 0x1112753b8>

# 返回的是迭代后的值
In [28]: for i in foo2():
    ...:     print(i)
    ...:
0
1
2
3
4

In [29]: foo3()
Out[29]: <generator object foo3 at 0x111275f10>

# 返回的是迭代后的值
In [30]: for i in foo3():
    ...:     print(i)
    ...:
0
1
2
3
4

事实上,yield from iterable就是for item in iterable: yield item的语法糖。

注意yield from 后面一定是iterable。

上一篇下一篇

猜你喜欢

热点阅读