[Python-ideas] Yield-from: Nonexistent throw() and close()?

Jacob Holm jh at improva.dk
Sat Feb 21 15:08:54 CET 2009

Hi Bruce

Bruce Frederiksen wrote:
> Jacob Holm wrote:
>> I'd hate for this:
>> def foo():
>>    for i in xrange(5):
>>        yield i
>> to behave different from this:
>> def foo():
>>    yield from xrange(5)
> These two forms already behave differently when generators are used 
> (rather than xrange), why should they not also behave differently when 
> non-generators are used?
Not sure in what way you think they behave differently?  foo is a 
generator in both cases, and as such has a send method.
I am thinking of #2 as a simple rewrite/refactoring using the nifty new 
feature.   Why should foo().send('bar') ignore the value in #1 and raise 
an exception  in #2?

> "In the face of ambiguity, refuse the temptation to guess."
> I think that an exception makes more sense, otherwise, we are guessing 
> as to what the programmer intended by using send in your example.
I disagree.  The principle of least surprise tells me that #1 and #2 
should be the same.



More information about the Python-ideas mailing list