第一段代码:
def test():
print("开始啦")
a1 = yield 3
print("第一次",a1)
a2 = yield 2
print("第二次",a2)
t=test()
res=t.__next__()
print(res)
t.send("2131")
# res=t.__next__()
# t.send("123456")
print(res)
第一段结果
开始啦
3
第一次 2131
3
第二段代码
def test():
print("开始啦")
a1 = yield 3
print("第一次",a1)
a2 = yield 2
print("第二次",a2)
t=test()
res=t.__next__()
print(res)
t.send("2131")
res=t.__next__()
t.send("123456")
print(res)
从第一段结果写第二段代码预测结果为:
开始啦
3
第一次 2131
3
第二次 123456
2
实际结果为:
3
第一次 2131
第二次 None
File "E:/Python/pycharm/ggz/1.py", line 11, in <module>
res=t.__next__()
StopIteration
疑问:到底send函数除了改变yield的值,还运行哪段代码,把yield 1 到yield2之间,而且好像还相当于自动执行了一次next