Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14555
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-16 20:05
文章分类
文章存档

2014年(4)

我的朋友

分类: Python/Ruby

2014-11-02 17:03:48


点击(此处)折叠或打开

  1. def has_yield_from_bug():
  2.     class MyGen:
  3.         def __init__(self):
  4.             self.send_args = None
  5.         def __iter__(self):
  6.             return self
  7.         def __next__(self):
  8.             print('next')
  9.             return 42
  10.         def send(self, *what):
  11.             print('send function')
  12.             self.send_args = what
  13.             return None
  14.     def yield_from_gen(gen):
  15.         print('before yield')
  16.         yield from gen
  17.         #_iter = iter(gen)
  18.         #while True:
  19.         # try:
  20.         # item = next(_iter)
  21.         # s = yield item
  22.         # if s is not None and hasattr(gen, 'send'):
  23.         # gen.send(s)
  24.         # except StopIteration:
  25.         # break
  26.         print('after yield')
  27.     value = (1, 2, 3)
  28.     gen = MyGen()
  29.     
  30.     coro = yield_from_gen(gen)
  31.     print('before next')
  32.     next(coro)
  33.     print('after next , before send')
  34.     coro.send(value)
  35.     print('after send')
  36.     return gen.send_args != (value,)
  37. _YIELD_FROM_BUG = has_yield_from_bug()
  38. print(_YIELD_FROM_BUG)
运行结果为:

点击(此处)折叠或打开

  1. [Cambox@laptop ~]$ python34 python-test/test.py
  2. before next
  3. before yield
  4. next
  5. after next , before send
  6. send function
  7. after send
  8. False


阅读(224) | 评论(0) | 转发(0) |
0

上一篇:yield和yield from的用法示例

下一篇:没有了

给主人留下些什么吧!~~