Working around a lack of 'goto' in python
Jeff Epler
jepler at unpythonic.net
Mon Mar 8 09:50:18 EST 2004
[Attribution helpfully discarded by Mr. Binns] wrote:
> > Yuck. Bletch. Barf.
>
On Sun, Mar 07, 2004 at 09:09:58PM -0800, Roger Binns wrote:
> Care to expand on that?
Start with this code:
for x in range(10):
for y in range(10):
if hit(x, y): break 2
Now, add z:
for x in range(10):
for y in range(10):
for z in range(10):
if hit(x, y, z): break 2
Look, a bug!
With this "feature", you have to scan the entire body of the loop
looking for a numeric break/continue if you're going to change the
indentation level, then increment or decrement the number.
If somebody was holding a gun to my head, I'd suggest that a way to name
loops was preferable to both this idiom and having my brains blown out:
for x in range(10) label outermost:
(other intervening control statements)
break outermost
I believe this abomination can even be done without actually reserving
"label" as a keyword, just like "from" in "import ... from", except for
the problem that the somewhat perverse
for x in 1, label outermost:
would be 2-tuple (1, label) followed by an illegal token "outermost".
.. and in the absence of coercive force, I'd -1 this suggestion too.
Jeff
More information about the Python-list
mailing list