How to break out of two nested for loops?
donn at u.washington.edu
Fri Jan 25 02:00:58 CET 2002
Quoth Gareth.McCaughan at pobox.com (Gareth McCaughan):
| Few things are *purely* stylistic. What we call a stylistic
| preference is generally a particular way to resolve the
| tradeoff between competing pressures. Those pressures are
| different for different people (on account of what they're
| used to seeing, or how their brain is built, or whatever),
| so different "stylistic" preferences can actually be good
| rational reasons for different people to act as they do.
| For some people, "unexceptional" use of exceptions is bad
| style. That means that they find it hard to follow, or that
| they are bothered by the can't-always-tell-where-it's-come-from
| nature of an exceptional control transfer. (That's why
| exceptions are a little like gotos.) For others, exceptions
| are a tool to be used whenever they're useful. Neither "side"
| is just plain right or just plain wrong.
| If "the issue" were: What should I do when I need to break
| out of two nested loops? then I agree: there would be no
| point whatever to this discussion. I think there was another
| issue: would it be better if Python had some mechanism
| for breaking out of nested loops that clearly and unambiguously
| said what it was doing? You can't resolve this issue just
| by saying "We have a mechanism for this, and it works OK";
| that argument would lead to the conclusion that we don't
| need ordinary single-level "break" either.
Oh, I don't know, seems to me "break" itself is not well served by
the exception option. An exception escapes to somewhere, and the
loop structure doesn't really matter; break has no real destination,
just "end of loop" wherever that may be.
Now if that's what we want, by analogy but extended to N enclosing
loops, like "break 2" - then an exception would be a crude substitute.
I think that would be kind of an awful construct, though - fragile,
because if you change the loop nesting you have to recalculate all
the break N's.
I thought the desire was to break out of 2 loops to get to a defined
place, really break out of however many loops it takes to get there.
For that, an exception is OK. The notion of an "unexceptional" use
of exceptions really does seem wrong to me - I mean, it's indefensible.
Any number of people who can hold this notion in their head do not make
it right. The intent to cling to this notion regardless of any rationale
is an attitude.
Honestly I'm not sure I have ever actually used an exception to break
out of 2 loops, even though I do need to do that often enough. I blame
myself, apparently too dull to think of it. I'm more interested in the
general topic, I guess how we think about how we thing about programming.
Are people's brains equipped with a natural facility to perceived
exception handling in a particular way? Is it nature - or nurture?
If the attitude about exceptions actually comes from some other language,
e.g., C++, it would not be the first time that has happened.
Donn Cave, donn at u.washington.edu
More information about the Python-list