How to break out of two nested for loops?

Gareth McCaughan Gareth.McCaughan at pobox.com
Wed Jan 23 19:19:39 EST 2002


Justin Sheehy wrote:

> >> The for-loop construct in Python uses exceptions for termination.  Doing
> >> so by hand is clearly in line with language idiom.
> >
> > The for-loop construct uses exceptions for termination
> > *internally*. That indicates that one common objection
> > to using exceptions for "normal" control flow doesn't
> > apply here (in some languages exceptions might be very
> > expensive), but it doesn't say anything about whether
> > it's good or bad style.
...
> The analogy doesn't fully hold, as Python intentionally exposes this
> to the programmer.  This is in line with the general attitude of
> interface-based programming in Python.
...
> You don't have to be "confronted with" this if you only want to use
> the builtin control flow structures on builtin items, but if you want
> to iterate over user-defined objects you quickly see that exceptions
> are the obviously encouraged means to do so.  The recent addition of
> iterators makes this even more explicit.

Very true. But for most of us (so I conjecture, anyway) that's
a relatively small part of the programming we do. I don't think
it's terrible if someone prefers, on stylistic grounds, to use
exceptions only (1) for truly exceptional situations and (2) as
a handy building block for higher-level things which one can use
without seeing the exceptions.

I'm not saying that no one should use exceptions for ordinary
control flow in Python. I'm saying that if someone is uncomfortable
about doing that then that doesn't indicate that their brain is
hopelessly un-Pythonic. That's all.

(But wasn't the discussion about whether using exceptions
that way isn't in fact confusing? Isn't that more than a
stylistic matter? Yes and no. What's confusing depends on
what you're used to. It's reasonable not to be used to a
heavily exception-based style even if you're writing Python,
and in that case using exceptions for normal control flow
will confuse you, and in that case you should avoid doing it.
<0.5 wink>)

> I disagree, again because we are not talking about the _inner_
> workings of the for loop but rather how it works in the language as
> exposed to the programmer.  The "workings" here is not the
> implementation but rather the interface.

Yes, it's an interface; yes, it's a valuable and elegant one;
yes, this stuff is a large part of what makes Python so good
(I'm saying this because I have this feeling that someone might
think I'm trying to marginalize one of the best things about
Python, which I'm not). But no, I don't think that means that
whatever is natural when coding to that interface is necessarily
good Python style elsewhere.

That would be true for interfaces that get used all the time
(say, the "other side" of the sequence interface, where you
say "for x in container: ..." and "thing[2:-5]" and so on),
but (unless my usage of Python is very atypical, which is
always possible) the implementation side of the sequence
interface is used much less.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
.sig under construc



More information about the Python-list mailing list