yield
博客专区 > hyhlinux 的博客 > 博客详情
yield
hyhlinux 发表于5个月前
yield
  • 发表于 5个月前
  • 阅读 6
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 证明: x = yield 表达式, 在调用next后,表达式被运行,但是赋值不会进行,直到协程被再次唤醒。 .

? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.

In [1]: c = 0

In [2]: def simple_core(): ...: global c ...: print('start c:', c) ...: c = yield (2+4) ...: print('end c:', c) ...:

In [3]: from inspect import getgeneratorstate

In [4]:

In [4]: my_coro = simple_core()

In [5]: getgeneratorstate(my_coro) Out[5]: 'GEN_CREATED'

In [6]: my_coro Out[6]: <generator object simple_core at 0x7f76f7cfff10>

In [7]: print(c) 0

In [8]: next(my_coro) start c: 0 Out[8]: 6

In [9]: print(c) 0

In [10]: getgeneratorstate(my_coro) Out[10]: 'GEN_SUSPENDED'

In [11]: my_coro.send(12) end c: 12

StopIteration Traceback (most recent call last) <ipython-input-11-2fa1746a2192> in <module>() ----> 1 my_coro.send(12)

StopIteration:

In [12]: In [7]: next(my_coro) ('start c:', 0) Out[7]: 5

In [8]: c Out[8]: 0

In [9]: my_coro.send(12) ('end c:', 12)

StopIteration Traceback (most recent call last) <ipython-input-9-2fa1746a2192> in <module>() ----> 1 my_coro.send(12)

StopIteration:

In [10]:

标签: python yield
共有 人打赏支持
粉丝 8
博文 130
码字总数 50509
×
hyhlinux
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: