"Clark C. Evans" <> writes:
> There is an interesting difference between how exceptions are handled
> between iterators constructed from a class, and iterators constructed
> from a generator. The following are "mostly equivalent":
[...]
> However, when exceptions are involved, behavior can be different:
(It would have been nice if you'd explained what the differences were
in your post, and given that you'd didn't if you'd made sure the
examples were correct -- got a NameError for iterable...)
Here's a better illustration:
>>> def iterable_via_generator():
.... yield "one"
.... raise Exception()
.... yield "two"
....
>>> i = iterable_via_generator()
>>> i.next()
'one'
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in iterable_via_generator
Exception
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
I agree this is surprising. I am vaguely aware of some effort being
put in to make generators "stick", i.e. this doesn't surprise me so
much:
>>> def g():
.... yield 1
.... return
.... yield 2
....
>>> i = g()
>>> i.next()
1
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
>>> i.next()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
I'd guess the behaviour you've noticed is an unintended consequence of
this. I'd also be willing to be persuaded that it's a bug, although
others might disagree, dunno. File an issue, anyway?
Cheers,
mwh
--
I would hereby duly point you at the website for the current pedal
powered submarine world underwater speed record, except I've lost
the URL. -- Callas, cam.misc
|