我无法理解这种send方法.我知道它用于操作发电机.但语法在这里:generator.send(value).
我莫名其妙地无法理解为什么值应该成为当前yield表达式的结果.我准备了一个例子:
def gen():
for i in range(10):
X = yield i
if X == 'stop':
break
print("Inside the function " + str(X))
m = gen()
print("1 Outside the function " + str(next(m)) + '\n')
print("2 Outside the function " + str(next(m)) + '\n')
print("3 Outside the function " + str(next(m)) + '\n')
print("4 Outside the function " + str(next(m)) + '\n')
print('\n')
print("Outside the function " + str(m.send(None)) + '\n') # Start generator …Run Code Online (Sandbox Code Playgroud)