[Python-Dev] PEP 380 (yield from a subgenerator) comments

Ron Adam rrr at ronadam.com
Mon Mar 30 20:06:50 CEST 2009



Ron Adam wrote:
> 
> 
> P.J. Eby wrote:
> 
>> Sure.  But right now, the return value of a generator function *is the 
>> generator*.  And you're free to ignore that, sure.
>>
>> But this is a "second" return value that only goes to a special place 
>> with special syntax -- without that syntax, you can't access it.
>>
>> But in the use cases where you'd actually want to make such a function 
>> return a value to begin with, it's because that value is the value you 
>> *really* want from the function -- the only reason it's a generator is 
>> because it needs to be paused and resumed along the way to getting 
>> that return value.
> 
> 
> How about if 'yield from' returns the generator object, and the return 
> value is accessed with an attribute.
> 
>    g = yield from gen
>    x = g.__value__
> 
> Or
> 
>    x = (yield from gen).__value__
> 
> 
> 
> Another possibility is to be able to break from a 'yield from' at some 
> point and then continue it to get any final values.
> 
>    # yield values of sub generator
>    g = yield from gen
> 
>    # get remaining unused value of sub generator
>    x = g.next()
> 

This could possibly be done in one line as well...

      x = (yield from gen).next()




More information about the Python-Dev mailing list