python3.6:AttributeError: 'generator' object has no attribute 'next'


環境:PyCharm+Anaconda

python版本:3.6

協程測試:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import time
 4 
 5 
 6 def consumer():
 7     r = ''
 8     while True:
 9         n = yield r
10         if not n:
11             return
12         print('[CONSUMER] Consumeing %s...' % n)
13         time.sleep(1)
14         r = '200 OK'
15 
16 
17 def produce(c):
18     c.next()
19     n = 0
20     while n < 5:
21         n = n + 1
22         print('[PRODUCER] Producing %s...' % n)
23         r = c.send(n)
24         print('[PRODUCER] Consumer return: %s' % r)
25     c.close()
26 
27 
28 if __name__ == '__main__':
29     c = consumer()
30     produce(c)

編譯報錯:

 

原因:在python 3.x中 generator(有yield關鍵字的函數則會被識別為generator函數)中的next變為__next__了,next是python 3.x以前版本中的方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM