yield for python
a example of code
def fun():
for i in range(20):
x = yield i
print('good', x, i)
if __name__ == '__main__':
a = fun()
a.__next__()
a.__next__()
x1 = a.send(5)
x2 = a.send(3)
next(a)
next(a)
print(x1, x2)
results
good None 0
good 5 1
good 3 2
good None 3
good None 4
2 3
description
yield generator functions allow you to declare a function that behaves like an iterator.
本文深入探讨了Python中yield关键字的功能,通过示例代码展示了如何使用yield创建生成器函数,解释了生成器函数如何像迭代器一样工作,以及如何在函数执行过程中发送和接收值。
1717

被折叠的 条评论
为什么被折叠?



