Working around a lack of 'goto' in python

Stephen Horne steve at ninereeds.fsnet.co.uk
Sun Mar 7 22:30:00 EST 2004


On Sun, 7 Mar 2004 14:33:38 -0800, "Roger Binns"
<rogerb at rogerbinns.com> wrote:

>Brett wrote:
>> Two areas where I've found 'goto' two be useful in other languages are in
>> (untested examples in C++)
>
>What you have is what many other languages allow with integers after break or
>continue statements.  For example you can do 'break 2' or 'continue 3' to
>break or continue out of the respective number of enclosing for loops.

Which languages?

The nearest I've seen is in Ada, where it is possible (but not normal
practice) to name loops. The equivalent of 'break' can then name which
loop it is referring to. IIRC, an example might be...

outer: loop
  inner: loop
    ...
    exit when ...;  --  by default, exit only the inner loop
    exit inner when ...;  --  explicitly exit the inner loop
    exit outer when ...;  --  explicitly exit both the inner and outer
                          --  loops
  end loop;
end loop;

I can't remember ever needing this, but then it's nearly 6 years since
I last used Ada in anger.


'break 2' is not something that I've seen, though it wouldn't surprise
me as a vendor specific extension in C or C++, or maybe as a Perl
thing.

My first impression of it is that it is probably a maintenance
nightmare. If you are using break to exit several nested loops, then
the break statement is likely obscured by other code. It isn't likely
to stand out to maintainers. When a new level of nesting gets added,
or a level of nesting gets taken out, during maintenance the
maintainer could easily miss the need to change the number on the
break statement.

Of course this can be an issue with any break/continue from a loop,
but these statements can been used relatively safely in small loops
where they will be seen. The programmer is responsible for using
language features appropriately and not otherwise. The thing is, I'm
not convinced there can be an appropriate case for breaking several
nested loops that way.

In Ada, at least the loops are explicitly named - adding or removing a
layer of nesting will not change which loop the exit statement
actually exits. Though even then, I don't remember ever seeing it used
for real.

Also, the thought of someone specifying the integer using a variable
or expression is somehow making me think of gothic castles, dark
stormy nights, thunder and lightening, and the distant howling of
wolves. I'm thinking 'I'd much rather be there than debugging a mess
like that' ;-)


>I don't like the rewriting as it greatly complicates the code, and you are
>having to write something different than what you mean in your head.
>Maybe someone wants to take this on as a PEP?

We need a real use case. I can't imagine needing the feature you
describe.

Sometimes, people get into coding habits using what they know just
because it is there and not realising that there are better ways to
handle it. Often, even 'what you mean in your head' gets imagined that
way purely as result of a bad habit. Sometimes you need to make what
amounts to a paradigm shift. I've been there and done that too many
times.

Maybe I'm wrong, but I'm very suspicious about this right now.

If I am wrong (perhaps it's me that needs to do the paradigm shift
thing after all) then I'd much rather Python went with the Ada route
of naming loops rather than adding gotos or allowing a
number-of-levels parameter for break and continue.


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk



More information about the Python-list mailing list