-1

I have a generator that I can send values to using the generator.send() method. I'd like to add to this generator after it has been made and continue using the same functions to iterate through it. I tried itertools.chain, but that returns an iterator and I get an AttributeError when I try to send values to it.

How can I combine two generators and preserve the ability to send values to them?

sjeknic
  • 15
  • 5

1 Answers1

2

You can use yield from (refer to the docs and this answer for more details):

def multi_generator(*gens):
    for gen in gens:
        yield from gen
a_guest
  • 34,165
  • 12
  • 64
  • 118