can generators be nested?

news.west.cox.net mark at hahnca.com
Wed Aug 27 23:51:10 EDT 2003


This doesn't work, but is there any elegant way to do something like this?

def gen2():
   yield "hello"

def gen1():
   gen2()
   yield "world"

for i in gen1(): print i

---- output ----
hello
world

I'm doing it this way now:

def gen2():
   yield "hello"

def gen1():
   for i in gen2(): yield i
   yield "world"






More information about the Python-list mailing list