[Python-ideas] sentinel_exception argument to `iter`

Chris Angelico rosuav at gmail.com
Fri Feb 7 03:45:50 CET 2014


On Fri, Feb 7, 2014 at 1:36 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> def __next__(self):
>   try:
>     x = self.func()
>   except Exception as exc:
>     if isinstance(exc, self.sentinel):
>       raise StopIteration from None
>   if x == self.sentinel:
>     raise StopIteration
>   else:
>     return x

Forgive me if this is a stupid question, but wouldn't this suppress
any other thrown exception? I'm looking for a bare 'raise' inside the
except block, such that any exception is guaranteed to raise
something, but if it's a subclass of self.sentinel, it raises
StopIteration instead.

ChrisA


More information about the Python-ideas mailing list