Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > goto

Reply
 
 
Hayri ERDENER
Guest
Posts: n/a
 
      07-18-2005
hi,
what is the equivalent of C languages' goto statement in python?
best regards
 
Reply With Quote
 
 
 
 
Comcast
Guest
Posts: n/a
 
      07-18-2005
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.


"Hayri ERDENER" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
hi,
what is the equivalent of C languages' goto statement in python?
best regards


 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      07-18-2005
Hayri ERDENER wrote:
> 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
 
Reply With Quote
 
Kay Schluehr
Guest
Posts: n/a
 
      07-18-2005

Hayri ERDENER schrieb:
> 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)ass
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

 
Reply With Quote
 
Gerhard Haering
Guest
Posts: n/a
 
      07-18-2005
On Mon, Jul 18, 2005 at 08:40:16AM -0700, Kay Schluehr wrote:
> Hayri ERDENER schrieb:
> > 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 - http://www.velocityreviews.com/forums/(E-Mail Removed) - Python, web & database development

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

iD8DBQFC29EEdIO4ozGCH14RAvXeAKC5p8zGuOeI6OcsRNaS5n q+k9/ZyQCgtA2n
ngCyiXRD5Z8PIauStFNBwKg=
=+91Q
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      07-18-2005
On Mon, 18 Jul 2005 14:48:08 +0300, Hayri ERDENER
<(E-Mail Removed)> declaimed the following in comp.lang.python:

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


A Google search on "Python comefrom" has a few hits...

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      07-18-2005
Hayri ERDENER wrote:
> what is the equivalent of C languages' goto statement in python?


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
 
Reply With Quote
 
rbt
Guest
Posts: n/a
 
      07-18-2005
On Mon, 2005-07-18 at 12:27 -0600, Steven Bethard wrote:
> Hayri ERDENER wrote:
> > what is the equivalent of C languages' goto statement in python?

>
> 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?

 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      07-18-2005
rbt wrote:
> On Mon, 2005-07-18 at 12:27 -0600, Steven Bethard wrote:
> > Hayri ERDENER wrote:
> > > what is the equivalent of C languages' goto statement in python?

> >
> > 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.

>
> 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.

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      07-18-2005
rbt wrote:
> Steven Bethard wrote:
>
>>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.

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL Goto statement ? Skybuck Flying VHDL 9 08-26-2005 01:46 PM
Re: VHDL Goto statement ? Skybuck Flying VHDL 0 08-08-2005 03:21 AM
where does Console.WriteLine() goto in a web app? Flip ASP .Net 1 04-14-2005 08:01 PM
where does Console.WriteLine() goto? Flip ASP .Net 6 11-18-2004 06:05 PM
goto statement is recommened in systemc? youngsun park VHDL 2 11-18-2003 03:47 PM



Advertisments