[Python-ideas] Add stop=exception option to next() (was Re: PEP 479 and take())

Raymond Hettinger raymond.hettinger at gmail.com
Sun Dec 14 00:56:15 CET 2014


> On Dec 13, 2014, at 2:36 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 
> On Sat, 13 Dec 2014 17:31:36 -0500
> Terry Reedy <tjreedy at udel.edu> wrote:
> 
>> I once proposed, either here or on python-list, and propose again,
>> that the signature of next be expanded so that the user could specify 
>> the ending exception.  If possible, the stop object could either be an 
>> exception class, which would be called with a generic message, or an 
>> exception instance.
>> 
>> Then the awkward
>> 
>> try:
>>   item = next(it)
>> except StopIteration:
>>   raise ValueError('iterable must not be empty') from None
> 
> I don't remember ever needing to write such code.

I've also never needed or desired this.

Several people on the list have become obsessed with the
notion of "leaking StopIteration" and there seem to be no
end of proposals that fight the basic mechanics of 
"exceptions propagate upwards until caught".

Everyone, please, please stop directing so much effort to
revise the fundamentals of the language -- it's NOT broken.
The iterator protocol and exceptions have been around 
for a long time.  They have been one of Python's greatest
success stories (not just in Python, but in many other
languages as well).


Raymond





More information about the Python-ideas mailing list