简体   繁体   English

如何在 Python 中将变量从一个生成器传递到另一个生成器?

[英]How can I pass a variable from one generator to another in Python?

I have the following 2 generators:我有以下 2 个生成器:

a = 20

def function1():
    coin = np.random.randint(0, 100)
    a2= a+coin
    yield a2


def function2():
    yield a2

I want to pass a2 to function2 .我想将a2传递给function2 How can I achieve this?我怎样才能做到这一点?

I am not completly sure if this is what you are looking for, but generators have a method called 'send' which enables you to pass data to the generator.我不完全确定这是否是您要查找的内容,但是生成器有一个名为“发送”的方法,可以让您将数据传递给生成器。 You might want to read this example .您可能想阅读此示例 Pay attention to the functions 'consume' and 'produce'.注意“消费”和“生产”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM