| Store | Cart

Working around a lack of 'goto' in python

From: Stephen Horne <ste...@ninereeds.fsnet.co.uk>
Mon, 08 Mar 2004 03:30:00 +0000
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

Recent Messages in this Thread
Brett Mar 06, 2004 06:18 pm
Andrew Koenig Mar 06, 2004 06:30 pm
Carmine Noviello Mar 06, 2004 06:41 pm
Jeff Schwaber Mar 06, 2004 06:41 pm
Peter Otten Mar 06, 2004 06:48 pm
Rene Pijlman Mar 06, 2004 06:49 pm
Christian Tismer Mar 06, 2004 08:50 pm
Roy Smith Mar 06, 2004 09:16 pm
Stephen Horne Mar 08, 2004 05:01 am
Dan Bishop Mar 06, 2004 11:23 pm
David M. Cooke Mar 07, 2004 08:49 am
Stephen Horne Mar 07, 2004 06:35 pm
Roy Smith Mar 07, 2004 06:54 pm
Stephen Horne Mar 07, 2004 07:57 pm
Roy Smith Mar 07, 2004 08:49 pm
Stephen Horne Mar 08, 2004 02:54 am
benjamin schollnick Mar 08, 2004 03:38 am
Stephen Horne Mar 08, 2004 05:10 am
Roger Binns Mar 07, 2004 10:33 pm
Roy Smith Mar 08, 2004 02:11 am
Roger Binns Mar 08, 2004 05:09 am
Georgy Mar 08, 2004 09:09 am
Jeff Epler Mar 08, 2004 02:50 pm
Mel Wilson Mar 08, 2004 03:43 pm
Roger Binns Mar 08, 2004 08:43 pm
Donn Cave Mar 08, 2004 10:46 pm
Roger Binns Mar 09, 2004 12:11 am
Stephen Horne Mar 08, 2004 03:30 am
Stephen Horne Mar 08, 2004 05:29 am
Y2KYZFR1 Mar 08, 2004 04:41 pm
Lou Pecora Mar 08, 2004 04:59 pm
Y2KYZFR1 Mar 09, 2004 04:36 pm
Joe Mason Mar 09, 2004 04:57 pm
Roy Smith Mar 09, 2004 05:25 pm
Joe Mason Mar 09, 2004 06:53 pm
Roy Smith Mar 09, 2004 09:05 pm
Roger Binns Mar 10, 2004 04:23 am
Roy Smith Mar 10, 2004 01:52 pm
Isaac To Mar 10, 2004 05:36 am
Jeff Epler Mar 10, 2004 01:05 pm
Isaac To Mar 10, 2004 03:58 pm
gabor Mar 10, 2004 04:34 pm
Donn Cave Mar 10, 2004 05:49 pm
Stephen Horne Mar 11, 2004 03:02 am
Jacek Generowicz Mar 11, 2004 11:40 am
Stephen Horne Mar 11, 2004 02:50 pm
Stephen Horne Mar 09, 2004 09:43 pm
Roy Smith Mar 09, 2004 09:55 pm
Stephen Horne Mar 10, 2004 12:41 am
Isaac To Mar 10, 2004 05:16 am
Christopher A. Craig Mar 08, 2004 07:54 pm
leeg Mar 09, 2004 01:34 am
Stephen Horne Mar 09, 2004 09:52 pm
leeg Mar 10, 2004 02:46 pm
Stephen Horne Mar 10, 2004 04:58 pm
Roger Binns Mar 10, 2004 06:14 pm
David MacQuigg Mar 09, 2004 11:26 pm
Anton Vredegoor Mar 10, 2004 10:11 am
Messages in this thread