This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mgiuca
Recipients carlj, mgiuca
Date 2008-07-10.16:05:08
SpamBayes Score 0.0001873626
Marked as misclassified No
Message-id <1215705910.76.0.273696569479.issue3331@psf.upfronthosting.co.za>
In-reply-to
Content
You seem to be suggesting that a StopIteration raised in the body of a
for-loop should cause the loop to break. That isn't (as far as I know)
the point of StopIteration. StopIteration is only used to break the
for-loop when raised by the iterator, not the body.

Hence I think the list comprehension is behaving correctly, as the
for-loop is, in that they are both raising the StopIteration you threw,
not catching it. That's valid, because you didn't throw it in the
iterator, you threw it in the condition.

What's more strange (to me) is the fact that the generator expression
stops when it sees a StopIteration. Note that this also happens if you
do it in the head of the generator expression. eg

def f(x):
    if x > 5:
            raise StopIteration
    return x

>>> list((f(x) for x in range(0, 100)))
[0, 1, 2, 3, 4, 5]

However, if you translate that into the full generator function version:

def my_generator_expr():
    for x in range(0, 100):
        yield f(x)

You see that it is behaving correctly.

So I think you discovered an interesting quirk, but it's hard to say
anything here is misbehaving.

By the way this is not a new issue with Python 3.0. Flagging it as a
Python 2.5 issue as well.
History
Date User Action Args
2008-07-10 16:05:10mgiucasetspambayes_score: 0.000187363 -> 0.0001873626
recipients: + mgiuca, carlj
2008-07-10 16:05:10mgiucasetspambayes_score: 0.000187363 -> 0.000187363
messageid: <1215705910.76.0.273696569479.issue3331@psf.upfronthosting.co.za>
2008-07-10 16:05:10mgiucalinkissue3331 messages
2008-07-10 16:05:08mgiucacreate