相关疑难解决方法(0)

Python 3:发送方法

我无法理解这种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)

python generator python-3.x

27
推荐指数
4
解决办法
1万
查看次数

标签 统计

generator ×1

python ×1

python-3.x ×1