Yielding a chain of values

Talin talin at acm.org
Sun Aug 28 13:29:06 EDT 2005


I'm finding that a lot of places within my code, I want to return the 
output of a generator from another generator. Currently the only method 
I know of to do this is to explicitly loop over the results from the 
inner generator, and yield each one:

        for x in inner():
            yield x

I was wondering if there was a more efficient and concise way to do 
this. And if there isn't, then what about extending the * syntax used 
for lists, i.e.:

    yield *inner()

The * would indicate that you want to iterate through the given 
expression, and yield each value in turn. You could also use it on 
ordinary lists:

    yield *[1, 2, 3 ]

Anyway, just an idle thought on a Sunday morning :)




More information about the Python-list mailing list