[Python-ideas] sentinel_exception argument to `iter`
Terry Reedy
tjreedy at udel.edu
Fri Feb 7 07:03:16 CET 2014
On 2/6/2014 11:15 PM, Terry Reedy wrote:
>> On Fri, Feb 7, 2014 at 1:36 PM, Terry Reedy
>>> def __next__(self):
>>> try:
>>> x = self.func()
>>> except Exception as exc:
>>> if isinstance(exc, self.sentinel):
>>> raise StopIteration from None
> else:
> raise
I just realized that the above is unnecessarily complicated because the
expression that follows 'except' is not limited to a builtin exception
class name or tuple thereof. (I have never before had reason to
dynamically determine the exception to be caught.) So, using a third
parameter, replace the 5 lines with 2.
except self.stop_exception:
raise StopIteration from None
>>> if x == self.sentinel:
>>> raise StopIteration
>>> else:
>>> return x
--
Terry Jan Reedy
More information about the Python-ideas
mailing list