[Python-ideas] thoughts on generator.throw()

Steven D'Aprano steve at pearwood.info
Wed Mar 18 23:19:55 CET 2009


On Thu, 19 Mar 2009 06:55:41 am Arnaud Delobelle wrote:
> On 18 Mar 2009, at 18:07, George Sakkis wrote:
> > On Wed, Mar 18, 2009 at 1:42 PM, Leif Walsh <leif.walsh at gmail.com>
> >
> > wrote:
> >> On Wed, Mar 18, 2009 at 1:20 PM, Antoine Pitrou
> >>
> >> <solipsis at pitrou.net> wrote:
> >>> - raise should be decently fast, which is easier with a dedicated
> >>> bytecode.
> >>
> >> Why does raise have to be decently fast?
> >>
> >> In my average case, at least, it's encountered at most once per
> >> program execution.  Even if I was good about catching exceptions,
> >> the point is that they're _exceptional_ cases, so they shouldn't
> >> be happening very often.
> >
> > That's not always true; StopIteration comes to mind.
>
> But StopIteration is not usually raised explicitly.

It still has to be raised. It's not just StopIteration either, the 
iteration protocol also catches IndexError:

>>> class C(object):
...     def __getitem__(self, i):
...         if i < 3: return i
...         else: raise IndexError
...
>>> c = C()
>>> for i in c:
...     print i
...
0
1
2
>>>




-- 
Steven D'Aprano



More information about the Python-ideas mailing list