def h():
print ‘wen chuan’ #①
M = yield 5 #②
print m #③
D = yield 12 #④
w = h()
month = w.next()
day = w.send(“fighter”)
print ‘We will never the day: ‘,month,’ . ‘,day
yield 生成器,碰到yield,程序挂起,但没停止。
注意: 第一次必须用.next(),如mf = w.next()
第二次可以用.send(“传入参数”),如df = w.send(“fighter”)
说明:send(“传入参数”),yield 5(表达式) = 传入参数
想要取出5, 用function.send(“*“)
如 day = w.send(“fighter”)
本文通过一个具体的Python代码示例介绍了生成器(generator)的概念及其使用方式,包括如何使用yield关键字来创建生成器函数,如何利用.next()和.send()方法进行调用与传参,并解释了yield的工作机制。
2645

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



