[Python-Dev] Coroutines, generators, function calling

Andrew Koenig ark at acm.org
Thu Oct 20 01:17:05 CEST 2005


> We yield values from inside for loops all over the place, but the
> yielded value is very rarely just the index value (only 1 of 14 yields)
> , but something calculated from the index value, so the new syntax would
> not be useful, unless it was something that provided access to the index
> item as a variable, like:
> 
> yield foo(i) for i in x
> 
> which barely saves you anything (a colon, a newline, and an indent).
> (hey wait, isn't that a generator comprehension? 

Here's a use case:

	def preorder(tree):
		if tree:
			yield tree
			yield from preorder(tree.left)
			yield from preorder(tree.right)





More information about the Python-Dev mailing list