goto

H

Hayri ERDENER

hi,
what is the equivalent of C languages' goto statement in python?
best regards
 
C

Comcast

In python there is no goto statement. In C I use goto only in one case: to
exit more then one level of blocks (as a matter of fact, I always use goto
EXIT in C, where EXIT is the label of the end of the function).

In python you can mimic this by throwing an exception and catching it.
Exception should "know" the destination label name and the catch statement
should compare this name with its (catch statement) name.


hi,
what is the equivalent of C languages' goto statement in python?
best regards
 
P

Peter Hansen

Hayri said:
what is the equivalent of C languages' goto statement in python?

Steven offered the best reply here, in that he wondered what you
actually need this for. What usage of "goto" in C are you hoping to
emulate? It's a certainty that some other non-goto technique will be
more appropriate in Python.

-Peter
 
K

Kay Schluehr

Hayri said:
hi,
what is the equivalent of C languages' goto statement in python?
best regards

No, but some of goto's use cases can be covered by unconditional jumps
provided by exceptions.


Here is a C function using goto:

void main()
{
int i, j;

for ( i = 0; i < 10; i++ )
{
printf( "Outer loop executing. i = %d\n", i );
for ( j = 0; j < 2; j++ )
{
printf( " Inner loop executing. j = %d\n", j );
if ( i == 3 )
goto stop;
}
}
/* This message does not print: */
printf( "Loop exited. i = %d\n", i );
stop: printf( "Jumped to stop. i = %d\n", i );
}


And here is a Python equivalent using exception handling:

def main():
class stop(Exception):pass
try:
for i in range(10):
print "Outer loop executing. i = %d"%i
for j in range(2):
print " Inner loop executing. j = %d"%j
if i == 3:
raise stop
print "Loop exited. i = %d"%i # message does not print
except stop:
print "Jumped to stop. i = %d"%i


Regards,
Kay
 
G

Gerhard Haering

Hayri said:
hi,
what is the equivalent of C languages' goto statement in python?
best regards

No, but some of goto's use cases can be covered by unconditional jumps
provided by exceptions. [...]

I like the "named loops" concept of other HLL like Ada 95 or Java better
than either goto or exceptions. It allows you to use "break" and
"continue" for other than the innermost loops, too:

break; => break out of inner loop
break loop_name; => break out of named loop "loop_name"

OTOH it's not used *that* often, so I won't argue for including it in
Python ;)

-- Gerhard
--
Gerhard Häring - (e-mail address removed) - Python, web & database development

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFC29EEdIO4ozGCH14RAvXeAKC5p8zGuOeI6OcsRNaS5nq+k9/ZyQCgtA2n
ngCyiXRD5Z8PIauStFNBwKg=
=+91Q
-----END PGP SIGNATURE-----
 
R

rbt

Download the goto module:
http://www.entrian.com/goto/
And you can use goto to your heart's content. And to the horror of all
your friends/coworkers. ;)

STeVe

Shouldn't that be "to the horror of all your goto-snob friends."

IMO, most of the people who deride goto do so because they heard or read
where someone else did.

Many of the world's most profitable software companies (MS for example)
have thousands of goto statements in their code... oh the horror of it
all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
as these obviously ignorant companies?
 
D

Dan Bishop

rbt said:
Shouldn't that be "to the horror of all your goto-snob friends."

IMO, most of the people who deride goto do so because they heard or read
where someone else did.

Or because they've seen some BASIC programs written ca. 1980.
 
S

Steven Bethard

rbt said:
Shouldn't that be "to the horror of all your goto-snob friends."

IMO, most of the people who deride goto do so because they heard or read
where someone else did.

Many of the world's most profitable software companies (MS for example)
have thousands of goto statements in their code... oh the horror of it
all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
as these obviously ignorant companies?

Perhaps I should reiterate: ;) Oh, and ;)

The CPython source code is filled with gotos -- with a rough grep count,
I found over 2000. And, while I wouldn't care to inspect them all, I'd
guess that they're mostly quite appropriate. A lot of them look like
"goto fail", "goto finally", etc. and are used to handle error
conditions. Heck, the patch I wrote to add key= arguments to min() and
max() in Python 2.5 uses goto for exactly this purpose.

That said, I don't think my joke was really that inaccurate -- I have
yet to see a good use case for gotos in Python. Why use gotos when you
have an efficient exception handling mechanism?

STeVe
 
S

Steven D'Aprano

Shouldn't that be "to the horror of all your goto-snob friends."

IMO, most of the people who deride goto do so because they heard or read
where someone else did.

Or because they actually programmed in languages that used goto for flow
control. Some of us have even programmed in versions of BASIC that didn't
have FOR or WHILE loops, you had to roll your own out of gotos.

How about you? How many times have you had to maintain and debug spaghetti
code sprinkled with gotos all over the place?
Many of the world's most profitable software companies (MS for example)
have thousands of goto statements in their code... oh the horror of it
all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
as these obviously ignorant companies?

You know, I agree with you here. Why, just the other day I was discussing
computer systems with my Aunt Tilly, who wanted to buy a computer so she
could send emails to her friends and relatives. I suggested she buy a Mac,
and she said "Well, I thought about buying a Mac, but I'm afraid I can't
possibly use any software that wasn't written using goto. So I'm going to
buy a Windows machine. Do you recommend any anti-virus and anti-spyware
software that's easy to use?"
 
M

Mike Meyer

rbt said:
Many of the world's most profitable software companies (MS for example)
have thousands of goto statements in their code... oh the horror of it
all. Why aren't these enlightened-by-the-gods know-it-alls as profitable
as these obviously ignorant companies?

Because profitability has *nothing* to do with code quality, and
everything to do with marketing. MS, in particular, has done an
excellent job of divorcing code quality from their bottom line by
shuffling the bulk of the support work off to other companies:
hardware vendors who bundle MS software, system integrators, and
customers friends and family being very high on the list.

That they felt the need to do this speaks volumes about their code
quality.

<mike
 
F

Fernando Perez

Steven said:
Download the goto module:
http://www.entrian.com/goto/
And you can use goto to your heart's content. And to the horror of all
your friends/coworkers. ;)

STeVe

That is actually a _really_ cool piece of code, in terms of showing off the
kind of things which are possible in python if you're willing to be a little
sneaky.

Thanks for the pointer !

best

f
 
D

D H

Mike said:
Because profitability has *nothing* to do with code quality, and
everything to do with marketing. MS, in particular, has done an
excellent job of divorcing code quality from their bottom line by
shuffling the bulk of the support work off to other companies:
hardware vendors who bundle MS software, system integrators, and
customers friends and family being very high on the list.

That they felt the need to do this speaks volumes about their code
quality.

whoa whoa whoa! Discussing goto statements and Microsoft together is
like mixing dynamite and gasoline. We don't want this to explode into
some interminable argument; that's not the kind of thing people like to
see on comp.lang.python.

Oh by the way, boo has goto statements:
http://svn.boo.codehaus.org/trunk/tests/testcases/integration/goto-1.boo?view=auto

KABOOM
 
L

Leif K-Brooks

rbt said:
IMO, most of the people who deride goto do so because they heard or read
where someone else did.

1 GOTO 17
2 mean, GOTO 5
3 could GOTO 6
4 with GOTO 7
5 what GOTO 3
6 possibly GOTO 24
7 you! GOTO 21
8 that GOTO 18
9 really, GOTO 23
10 understandable?
11 neat. GOTO 16
12 and GOTO 25
13 are GOTO 9
14 I GOTO 26
15 wrong GOTO 20
16 I GOTO 2
17 Yes, GOTO 14
18 simple GOTO 12
19 agree GOTO 4
20 with GOTO 22
21 Gotos GOTO 13
22 something GOTO 8
23 really GOTO 11
24 be GOTO 15
25 easily GOTO 10
26 totally GOTO 19
 
S

Steven Bethard

Fernando said:
That is actually a _really_ cool piece of code, in terms of showing off the
kind of things which are possible in python if you're willing to be a little
sneaky.

Yeah, it's pretty slick. I think most people who see the link don't
realize that it's actually *working code* for gotos in Python.

STeVe
 
S

Sybren Stuvel

rbt enlightened us with:
Many of the world's most profitable software companies (MS for
example) have thousands of goto statements in their code... oh the
horror of it all. Why aren't these enlightened-by-the-gods
know-it-alls as profitable as these obviously ignorant companies?

They write software with huge security holes. Direct3D still isn't as
stable as OpenGL. It takes ages for them to create security patches.

The things I mention are *not* caused by their nice and clean way of
coding.

As a matter of fact, they use goto to jump from one function to
another! And to make sure a 'return' doesn't return to the last call,
but to some other, they combine this awful use of goto with manual
stack manipulation. And they do all of this in C (or some derivative)
so if one function changes it's parameters, all the manual stack
modifications and gotos need to be checked for correctness.

I'd rather use an exception, or better even - write small functions so
I can call 'return' instead of doing 'goto EXIT'.

Sybren
 
R

Robert Kern

rbt said:
IMO, most of the people who deride goto do so because they heard or read
where someone else did.

Or perhaps, like me, they have had to maintain FORTRAN code written by a
scientist who apparently hadn't heard of subroutines. "Spaghetti"
doesn't quite describe it. I've settled on "Lovecraftian": reading the
code, you can't help but get the impression of writhing tentacles and
impossible angles.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
R

Rocco Moretti

Leif said:
1 GOTO 17
2 mean, GOTO 5
3 could GOTO 6
4 with GOTO 7
5 what GOTO 3
6 possibly GOTO 24
7 you! GOTO 21
8 that GOTO 18
9 really, GOTO 23
10 understandable?
11 neat. GOTO 16
12 and GOTO 25
13 are GOTO 9
14 I GOTO 26
15 wrong GOTO 20
16 I GOTO 2
17 Yes, GOTO 14
18 simple GOTO 12
19 agree GOTO 4
20 with GOTO 22
21 Gotos GOTO 13
22 something GOTO 8
23 really GOTO 11
24 be GOTO 15
25 easily GOTO 10
26 totally GOTO 19

I dislike gotos because it is too easy to inadvertently create infinite
loops. <10 WINK; 20 GOTO 10>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top