Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Working around a lack of 'goto' in python (http://www.velocityreviews.com/forums/t329308-working-around-a-lack-of-goto-in-python.html)

 Brett 03-06-2004 06:18 PM

Working around a lack of 'goto' in python

Two areas where I've found 'goto' two be useful in other languages are in
(untested examples in C++)

(1) deeply nested loops

for (k=0; k < 10; ++k)
for (j=0; j < 10; ++j)
for (i=0; i <10; ++i)
if (/* some test */) goto END;

END: /* continue */;

and (2) repeating a while or for loop from the beginning:

BEGIN:
for (n=0; n < 20; ++n)
if (/* some test */) goto BEGIN;

What are the techniques in python for simulating these algorithms without a
'goto' command?

Thanks.

 Andrew Koenig 03-06-2004 06:30 PM

Re: Working around a lack of 'goto' in python

> What are the techniques in python for simulating these algorithms without
a
> 'goto' command?

How about showing us an actual piece of code in which you consider the lack
of a 'goto' to be a significant inconvenience?

 Carmine Noviello 03-06-2004 06:41 PM

Re: Working around a lack of 'goto' in python

> (1) deeply nested loops
>
> for (k=0; k < 10; ++k)
> for (j=0; j < 10; ++j)
> for (i=0; i <10; ++i)
> if (/* some test */) goto END;

done = 0
while k < 10 and not done:
k += 1
while j < 10 and not done:
j+=1
while i < 10 and not done:
i+=1
if /*some test*/: done = 1

> and (2) repeating a while or for loop from the beginning:
>
> BEGIN:
> for (n=0; n < 20; ++n)
> if (/* some test */) goto BEGIN;
>

while n < 20:
if /*some test*/: n = 0

There is always a way to not use "goto".

--
Don't you know why your Python application has crashed?
Take a look to http://www.pycrash.org
My Home Page http://cnoviello.altervista.org

 Jeff Schwaber 03-06-2004 06:41 PM

Re: Working around a lack of 'goto' in python

On Sat, 2004-03-06 at 10:18, Brett wrote:
> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)
>
> (1) deeply nested loops
>
> for (k=0; k < 10; ++k)
> for (j=0; j < 10; ++j)
> for (i=0; i <10; ++i)
> if (/* some test */) goto END;
>
> END: /* continue */;

try:
for k in range(10):
for j in range(10):
for i in range(10):
if "some test": raise Exception("test")
except:
pass

> and (2) repeating a while or for loop from the beginning:
>
> BEGIN:
> for (n=0; n < 20; ++n)
> if (/* some test */) goto BEGIN;

for n in range(20):
if "some test": continue

Jeff

 Peter Otten 03-06-2004 06:48 PM

Re: Working around a lack of 'goto' in python

Brett wrote:

> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)
>
> (1) deeply nested loops

class BeamMeUpScotty(Exception):
pass

try:
for i in range(10):
for k in range(10):
for n in range(10):
if i == k == n == 2:
raise BeamMeUpScotty
except BeamMeUpScotty:
pass

print (i, k, n)

Ok, I'm only kidding, here's the real thing:

def beamMeUp():
for i in range(10):
for k in range(10):
for n in range(10):
if i == k == n == 2:
return i, k, n

print beamMeUp()

> and (2) repeating a while or for loop from the beginning:
>
> BEGIN:
> for (n=0; n < 20; ++n)
> if (/* some test */) goto BEGIN;

while 1:
for i in range(10):
print i,
if i == 5 and raw_input("restart?") != "no":
break
else:
break

Can't remember having done that in my code, though. If the need arises I'd
probably go with the function approach again. To me goto seems much like a
speed hack that is fine in, say, the kernel code, but by no means in a
scripting language.

Peter

 Rene Pijlman 03-06-2004 06:49 PM

Re: Working around a lack of 'goto' in python

Brett:
>What are the techniques in python for simulating these algorithms without a
>'goto' command?

While we're on the subject, is there any way to simulate the 'comefrom'
statement?
http://www.gre.ac.uk/~hj05/stuff/comefrom.htm

--
René Pijlman

 Christian Tismer 03-06-2004 08:50 PM

Re: Working around a lack of 'goto' in python

Rene Pijlman wrote:

> Brett:
>
>>What are the techniques in python for simulating these algorithms without a
>>'goto' command?

>
>
> While we're on the subject, is there any way to simulate the 'comefrom'
> statement?

Sure. If you grab old Stackless python 1.0 with Python 2.0,
you have first class continuations, and you can make your
code come from wherever you want :-)

It *is* possible, but this was no serious proposal. - chris

--
Christian Tismer :^) <mailto:tismer@stackless.com>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/

 Roy Smith 03-06-2004 09:16 PM

Re: Working around a lack of 'goto' in python

In article <UBo2c.26879\$pg4.12221@newssvr24.news.prodigy.com> ,
"Brett" <abc@def.net> wrote:

> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)

Working around the lack of "goto" in a language is kind of like trying
to figure out how to swim without a piano strapped to your back. There
are lots of ways to do it, and even the worst of them is better than the
alternative.

> (1) deeply nested loops
>
> for (k=0; k < 10; ++k)
> for (j=0; j < 10; ++j)
> for (i=0; i <10; ++i)
> if (/* some test */) goto END;
>
> END: /* continue */;

I've found two general solutions to the breaking out of a deeply nested
loop. One possibility is to factor the loop out into its own function,
and break out of it with a return:

def findTheHiddenThing ():
for k in range (10):
for j in range (10):
for i in range (10):
if f (i, j, k) == theThingWeWant:
return

Another is to use an exception:

try:
for k in range (10):
for j in range (10):
for i in range (10):
if f (i, j, k) == theThingWeWant:
raise WeFoundIt ("in the last place we looked")
except WeFoundIt:
whatever

Neither of these ideas are particularly Python specific. The first
method works in pretty much any procedural language. The second only
works in languages that support exceptions (in some languages, exception
processing is fairly expensive, so it may not be universally acceptable).

Assuming both methods are equally feasable in your environment, the
choice of which to use may be driven by style, or one may just be a
better fit to your problem domain.

On the other hand, if you're doing linear searches of multi-dimensional
coordinate spaces, that may be a hint that you need to rethink how
you're solving the problem. A different data structure may not only
eliminate the deeply nested loop, but could well be significantly faster
to execute.

> and (2) repeating a while or for loop from the beginning:
>
> BEGIN:
> for (n=0; n < 20; ++n)
> if (/* some test */) goto BEGIN;

I'm going to go out on a limb on this one and say that if you need to
begin a loop from the beginning again, something is probably wrong with
how you're trying to solve the problem. What you've really written is a
nested while/for loop. In C, it should look something like this:

while (1)
for (n = 0; n < 20; ++n)
if (some test)
break

and now you're back to the same sort of "breaking out of a nested loop"
issue like you had above, with the same likely solutions.

 Dan Bishop 03-06-2004 11:23 PM

Re: Working around a lack of 'goto' in python

"Brett" <abc@def.net> wrote in message news:<UBo2c.26879\$pg4.12221@newssvr24.news.prodigy .com>...
> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)
>
> (1) deeply nested loops
>
> for (k=0; k < 10; ++k)
> for (j=0; j < 10; ++j)
> for (i=0; i <10; ++i)
> if (/* some test */) goto END;
>
> END: /* continue */;

# One way is to fake GOTO using try-except

class END(Exception):
pass

try:
for k in xrange(10):
for j in xrange(10):
for i in xrange(10):
if some_test():
raise END()
except END:
pass

# Another is to put the nested loops inside a function

def nested_loops():
for k in xrange(10):
for j in xrange(10):
for i in xrange(10):
if some_test():
return

> and (2) repeating a while or for loop from the beginning:
>
> BEGIN:
> for (n=0; n < 20; ++n)
> if (/* some test */) goto BEGIN;

n = 0
while n < 20:
if some_test():
n = 0
continue
n += 1

> What are the techniques in python for simulating these algorithms without a
> 'goto' command?

 David M. Cooke 03-07-2004 08:49 AM

Re: Working around a lack of 'goto' in python

At some point, "Brett" <abc@def.net> wrote:

> Two areas where I've found 'goto' two be useful in other languages are in
> (untested examples in C++)
>
> (1) deeply nested loops
>
> for (k=0; k < 10; ++k)
> for (j=0; j < 10; ++j)
> for (i=0; i <10; ++i)
> if (/* some test */) goto END;
>
> END: /* continue */;

If you're not doing anything between the loops, I like to use a
generator:

def iindices(klimit, jlimit, ilimit):
for k in xrange(klimit):
for j in xrange(jlimit):
for i in xrange(ilimit):
yield k, j, i

for k, j, i in iindices(klimit, jlimit, ilimit):
if test_fails(k, j, i):
break

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca

All times are GMT. The time now is 02:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.