[Tutor] What is this an example of (and how can i use it?)
Alan Gauld
alan.gauld at btinternet.com
Sun Sep 20 18:32:14 CEST 2009
kevin parks wrote:
> called, and what it is an example of. I guess there are generators and
> iterators now and it seems this might be an example of one of those new
This is a generator expression. It is like a list comprehension (you
know about those right?) except it doesn't create the list it just
returns each item on demand. You could think of a list as a list
constructed using a generator expression.
> def roundrobin(*iterables):
> "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
> # Recipe credited to George Sakkis
> pending = len(iterables)
> nexts = cycle(iter(it).next for it in iterables)
note this is storing the next methods not the results of them.
> while pending:
> try:
> for next in nexts:
> yield next()
So the yield calls the stored method and returns the result.
HTH,
Alan G.
More information about the Tutor
mailing list