I yould like to have generators that defer to other generators, e.g.
def gx():
for i in [1, 2, 3]:
yield i
def gy():
for i in [11, 12, 13]:
yield i
def gz():
"""this should defer to gx and gy to
generate [1, 2, 3, 11, 12, 13]"""
for i in gx(): yield i
for i in gy(): yield i
Is the explicit loop in gz() the only way to do this, or are there better alternatives?
In currently released Python versions, an explicit loop is the only way to invoke sub-generators. (I presume your example is just, well, an example — not the exact problem you want to solve.)
Python 3.3 will add the special syntax
yield fromfor this purpose:See PEP 380 for further details.