[Python-ideas] x=(yield from) confusion [was:Yet another alternative name for yield-from]

Jacob Holm jh at improva.dk
Thu Apr 9 19:32:07 CEST 2009


Jim Jewett wrote:
> On 4/9/09, Nick Coghlan <ncoghlan at gmail.com> wrote:
>   
>> Jacob Holm wrote:
>>     
>>> Then let me revisit my earlier statement that when close() catches a
>>> StopIteration with a non-None value, it should either return it or raise
>>> an exception.
>>>       
>
> This implies that the value is always important; I write plenty of
> functions that don't bother to return anything, and expect that would
> still be common if I were factoring out loops.
>   

If you return None (or no value) you won't get an exception with my 
proposal.  Only if you handle a GeneratorExit by returning a non-None 
value.  Since that value is not going to be visible to any other code, 
it doesn't make sense to return it.  I am suggesting that you should get 
a RuntimeError so you become aware that returning the value doesn't make 
sense.

>>> Since the value is not saved, a second close() will
>>> neither be able to return it, nor raise a StopIteration with it.
>>>       
>
> If close is called more than once, that suggests at least one of those
> calls is just freeing resources, and doesn't need a value.
>
>   
>>>> It doesn't matter if there is only one use case, as long as it is a
>>>> common one. And we already have that: Greg Ewing's "refactoring".
>>>>         
>
>   
>>> I remain unconvinced that the "initial next()" issue isn't also a
>>> problem for that use case, but I am not going to argue about this.
>>>       
>
> That does suggest that yield-from *should* accept pre-started
> generators, if only because the previous line (or a decorator) may
> have primed it.
>
>   

That was my argument, but since there is no sane way of handling 
pre-primed generators without extending the PEP in a direction that 
Guido has forbidden, I suggest raising a RuntimeError in this case 
instead.   That allows us to add the necessary features later if the 
need is recognized.

>> For refactoring, the pattern of passing in a "start" value for use in
>> the first yield expression in the subiterator should be adequate.
>>     
>
> Only if you know what the first value should be.  If you're using a
> sentinel that you plan to discard, then it would be way simpler to
> just prime the generator before the yield-from.
>
>   

Yeah, but yield from with pre-primed generators just ain't gonna happen. :(


- Jacob




More information about the Python-ideas mailing list