生成器返回指定的值
需求:生成器传入字符串,返回字符串对应的数字
def genFunc():
x = None
while True:
print("call yield x=", x)
v = yield x
x = int(v)调用过程: 第一次:创建生成器,并执行一次
gen = genFunc() next(gen)
结果:
call yield x= None
第二次:
value = gen.send('10')
print(value)结果:
call yield x= 10 10