Working around a lack of 'goto' in python

Roger Binns rogerb at
Tue Mar 9 01:11:07 CET 2004

> I believe you'd be able to get the same benefit from goto -
> nearly identical solution, really.

Yes, break and continue of one or more levels can be written
using gotos.  'for' loops can be written using 'while'.

> Of course it can be used
> a lot of other ways, but you couldn't really argue that's a
> bad thing, given your position stated above.

I never did argue it is a bad thing.  I am not the OP.  The
OP has a problem with a lack of goto in Python, when what
they seemed to be trying to express was breaks and continues
out of multiple levels of nested loops.

I pointed out that instead of goto (which would require a new
language features) break/continue with labels or numbers
(which would require new language features) could be used
and they more clearly express what the programmer is trying
to do.  I was also showing that the current situation (introducing
variables, using exceptions) is an acceptable work around
but is just as error prone as break/continue.  goto is
less error prone, but is considered a bad thing for many
other reasons.

> I mean, either
> Python should incorporate all features that someone finds
> useful, and leave taste and judgement to the user, or it
> shouldn't.

That is a tautology.  Python has a set of features as determined
by BDFL influenced by the community.  It is very clear on helping
the programmer directly express what they mean.  That is why it
has 'for' loops even though 'while' would suffice.  That is why
it has multiple-inheritance although that is not necessary.  It
doesn't have a way of direclty expressing break/continue out of
multiple levels of nested loops.  The OP is used to expressing
that in C using goto.  Python people are used to introducing
extra variables and/or exceptions.  Other languages let you use
labelled or numbered break/continues.

I do believe some standard code pattern would be useful, based
on having needed to do it myself a non-trivial number of times
in my own code.  Other posters have also needed to do the same
as they came out with their ways of expressing it (although most
only showed break and not continue).

I don't actually care what the code pattern is.  Maybe it is
introducing gotos, or named/labelled break/continue, or the
status quo.

I am still stuck with Python 2.2 code.  Having this addressed
would be nice, but sadly it will be a few years before I could
use it.


More information about the Python-list mailing list