Yielding a chain of values

Bengt Richter bokr at oz.net
Wed Aug 31 03:01:50 EDT 2005


On Tue, 30 Aug 2005 21:23:39 -0400, Peter Hansen <peter at engcorp.com> wrote:

>Bengt Richter wrote:
>> On Tue, 30 Aug 2005 23:12:35 +0200, Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
>>>Bengt Richter wrote:
>>>>Maybe
>>>>         yield in inner()
>>>>
>>>>could be sugar for the above and become something optimized?
>>>
>>>The problem here is that yield isn't a statement any more. It's now an
>>>expression, so it is not easy to find new syntax around it.
>> 
>> No, the idea was that it's still a statement, but what it
>> yields is "in inner()" which UIAM is illegal now, and would
>> signify "whatever sequence of elements is in inner()" --
>> really yield in seq -- I don't know what inner() was, but I assumed
>> an iterable.
>
>I believe he was referring indirectly to 
>http://www.python.org/peps/pep-0342.html (see item #1 in the 
>"Specification Summary" section), where yield will become an expression. 
>  This PEP has been accepted, thus his use of present tense, confusing 
>though it is when it's not in the released version of Python yet.
>
>Or I might be wrong. ;-)
Well, maybe it's right both ways ;-) I.e., even though yield "is" now
an expression, it is valid to use it as an expression-statement which
evaluates the expression and discards the value. So I think you could
still use the currently illegal "yield in" token sequence to mean that
what follows is to be taken as an iterable whose full sequence is
to be yielded sequentially as if

    yield in iterable

were sugar for

    for _ in iterable: yield _   

Regards,
Bengt Richter



More information about the Python-list mailing list