标签:示例 har new 等价 python div tin ext value
#!/usr/bin/python2.7 def consumer(): while True: newn = yield print ‘Consumer : {}‘.format(newn) def producer(func): func.next() # 必须实例化,否则:TypeError: can‘t send non-None value to a just-started generator # func.send(None) 等价于 func.next() n = 0 while n < 5: print ‘Producer : {}‘.format(n) func.send(n) n += 1 func.close() if __name__ == ‘__main__‘: c = consumer() producer(c) # 结果如下: Producer : 0 Consumer : 0 Producer : 1 Consumer : 1 Producer : 2 Consumer : 2 Producer : 3 Consumer : 3 Producer : 4 Consumer : 4
#!/usr/bin/python2.7 def count_down(n=5): while n > 0: newn = yield n if newn: n = newn else: n -= 1 if __name__ == ‘__main__‘: c = count_down() for num in c: print num if 5 == num: c.send(3) # 结果如下: 5 2 1
标签:示例 har new 等价 python div tin ext value
原文地址:https://www.cnblogs.com/standby/p/9364544.html