yield_all needed in Python

Steve Holden steve at holdenweb.com
Tue Mar 1 11:25:29 CET 2005


Terry Reedy wrote:
> "Douglas Alan" <nessus at mit.edu> wrote in message 
> news:lck6osnu68.fsf at gaffa.mit.edu...
> 
>>    We can shorten the code--and make it run in O(N) time--by adding a 
>>new
>>    keyword to replace the "for v in ...: yield v" pattern:
> 
> 
> Maybe.  Until you define the semantics of yield_all and at least outline an 
> implementation, I am not convinced of 'run in o(n) time'.  There was once a 
> several-post discussion of a related idea of having yield somehow, 
> magically, skip intermediate generators that only yielded value on up, 
> without tranformation.  But it was never clear how to do this practically 
> without negatively impacting all generators.  Cetainly, if <yield_all 
> iterator> == <for i in iterator: yield i>, I don't see how anything is 
> gained except for a few keystrokes.  If <yield_all iterator> == <yield 
> list(i for i in iterator)> then the replacement is a semantic change.
> 
La plus ca change, la plus c'est la meme chose (I trust native French 
speakers will excuse the laziness that led to the absence of accent).

This is very reminiscent of discussions several years ago about tail 
recursion and how it would be a great thing to optimise the edge cases. 
Of course we didn't have generators then, so we couldn't complain about 
*their* inefficiencies then.
> 
>>      def in_order(self):
>>          if self.left is not None:
>>              yield_all self.left.in_order():
>>          yield self.value
>>          if self.right is not None:
>>              yield_all self.right.in_order():
> 
> 
> If and when I write a text-based double-recursion to iteration transformer, 
> a pseudokeyword might be be an idea for indicating that stacked yields are 
> identify functions and therefore bypassable.
> 
The key words in the above being "use" and "case", I suspect.

python:-always-something-new-to-bitch-about-ly y'rs  - steve




More information about the Python-list mailing list