[Python-ideas] yiled-from restrictions [was: x=(yield from) confusion]

Jim Jewett jimjjewett at gmail.com
Fri Apr 10 04:53:29 CEST 2009

[deleted file processing example, but Greg may want to add it to the
PEP as a clear motivating use case]

On 4/9/09, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Jacob Holm wrote:
> For more complex cases, once caching of the methods in yield-from is
> explicitly disallowed (as per my other message), you can do something like:

I thought part of the reason to use a generator instead of an iterator
class was speed/efficiency, which that constraint would seem to block.
(name lookups at each level)

>>>> def start(iterable, start):
> ...     itr = iter(iterable)
> ...     class TrapFirstNext(object):
> ...       def __iter__(self):
> ...         return self
> ...       def next(self):
> ...         TrapFirstNext.next = itr.next
> ...         return start
> ...     return TrapFirstNext()

And if that becomes a standard idiom, was anything really simplified?


More information about the Python-ideas mailing list