简体   繁体   中英

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

I have the following 2 generators:

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 . 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'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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