can't send non-None value to a just-started generator
can't send non-None value to a just-started generator 第一次调用生成器不会复制
1 def foo(): 2 print('OK') 3 4 count=yield 1 5 print(count) 6 7 # print('OK2') 8 # yield 2 9 b=foo() 10 # print(next(b)) 11 b.send(None) # 这句代码等同于 next(b) 12 # next(b) 13 # b.send(8)
结果:
"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/7.2/生成器2.py OK Process finished with exit code 0
出现报警现象是第一次调用就开始赋值:
1 def foo(): 2 print('OK') 3 4 count=yield 1 5 print(count) 6 7 # print('OK2') 8 # yield 2 9 b=foo() 10 # print(next(b)) 11 # b.send(None) # 这句代码等同于 next(b) 12 # next(b) 13 b.send(8)
结果:
"D:\Program Files (x86)\python36\python.exe" F:/python从入门到放弃/7.2/生成器2.py Traceback (most recent call last): File "F:/python从入门到放弃/7.2/生成器2.py", line 13, in <module> b.send(8) TypeError: can't send non-None value to a just-started generator Process finished with exit code 1