Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Good python equivalent to C goto

Reply
Thread Tools

Good python equivalent to C goto

 
 
Kurien Mathew
Guest
Posts: n/a
 
      08-16-2008
Hello,

Any suggestions on a good python equivalent for the following C code:

while (loopCondition)
{
if (condition1)
goto next;
if (condition2)
goto next;
if (condition3)
goto next;
stmt1;
stmt2;
next:
stmt3;
stmt4;
}



Thanks
Kurien
 
Reply With Quote
 
 
 
 
Thomas Mlynarczyk
Guest
Posts: n/a
 
      08-16-2008
Kurien Mathew schrieb:

> Any suggestions on a good python equivalent for the following C code:
>
> while (loopCondition)
> {
> if (condition1)
> goto next;
> if (condition2)
> goto next;
> if (condition3)
> goto next;
> stmt1;
> stmt2;
> next:
> stmt3;
> stmt4;
> }


while loopCondition:
if not( cond1 or cond2 or cond3 ):
stmt1
stmt2
stmt3
stmt4

Not tested.
Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux avoir tort qu'ils ont raison!
(Coluche)
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-16-2008
Kurien Mathew wrote:

> Any suggestions on a good python equivalent for the following C code:
>
> while (loopCondition)
> {
> if (condition1)
> goto next;
> if (condition2)
> goto next;
> if (condition3)
> goto next;
> stmt1;
> stmt2;
> next:
> stmt3;
> stmt4;
> }


seems as if you *don't* want to execute the first part if any of the
conditions are true. in other words,

while loopCondition:
if not (condition1 or condition2 or condition3):
stmt1
stmt2
stmt3
stmt4

</F>

 
Reply With Quote
 
Christian Heimes
Guest
Posts: n/a
 
      08-16-2008
Kurien Mathew wrote:
> Hello,
>
> Any suggestions on a good python equivalent for the following C code:
>


There are various ways to write your example in Python. For example

while loopCondition:
condition = 1
while condition:
if condition1:
break
if condition2:
break
if condition3:
break
stmt1
stmt2
condition = 0
else:
stmt3
stmt4

The else block of while isn't execute if you break out of while.

You can emulate multiple gotos with exceptions. In general you shouldn't
try to mimic C in Python code. C like Python code is often hard to read
and slower than well designed Python code.

 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      08-17-2008
Dennis Lee Bieber wrote:
> Nasty code even for C... I've never used goto in C... Options:
> convert the statements of next into a function, and put in an else
> clause...


I think the parent post's pseudocode example was too simple to show the
real benefits and use cases of goto in C. Obviously his simple example
was contrived and could easily be solved with proper if tests. Your
idea of using a function is correct for python, though, but not for C

However, what you say about else clauses gets extremely hairy when you
have to deal with multiple nested if statements. The most common use
case in C for goto is when you have cleanup code that must run before
leaving a function. Since any number of things could happen while
processing a function (at any level of the if statement) that would
trigger an exit, having a whole bunch of else statements gets very, very
tedious and messy. Often involves a lot of code duplication too. Goto
is direct and much cleaner and more readable.

A function in C wouldn't work for this because of scoping rules.

However it's not necessary in python to do any of this, since you can
define nested functions that have access to the parent scope. Anytime
you need to clean up, just call the nested cleanup function and then return.
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      08-17-2008
Kurien Mathew wrote:
> Hello,
>
> Any suggestions on a good python equivalent for the following C code:
>
> while (loopCondition)
> {
> if (condition1)
> goto next;
> if (condition2)
> goto next;
> if (condition3)
> goto next;
> stmt1;
> stmt2;
> next:
> stmt3;
> stmt4;
> }


I think the most direct translation would be this:

def whateverfunc():

def next_func():
stmt3
stmt4

while loopCondition:
if condition1:
next_func()
return
if condition2:
next_func()
return
if condition3:
next_func()
return
stmt1
stmt2




 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      08-17-2008
Michael Torrie wrote:
> I think the most direct translation would be this:


Nevermind I forgot about the while loop and continuing on after it.
Guess the function doesn't quite fit this use case after all.
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      08-17-2008
On Aug 17, 12:35*am, Michael Torrie <(E-Mail Removed)> wrote:
> However it's not necessary in python to do any of this, since you can
> define nested functions that have access to the parent scope. *Anytime
> you need to clean up, just call the nested cleanup function and then return.


That is unnecessary and dangerous in Python.

98% of the time the Python interpreter cleans up what you would have
had to clean up by hand in C.

The rest of the time you should be using a try...finally block to
guarantee cleanup, or a with block to get the interpreter do it for
you.

Calling a nested function to perform cleanup is prone to omission
errors, and it doesn't guarantee that cleanup will happen if an
exception is raised.


Carl Banks
 
Reply With Quote
 
info@orlans-amo.be
Guest
Posts: n/a
 
      08-17-2008
as an oldtimer, I know that in complex code the goto statement is
still the easiest to code and understand.

I propose this solution using exception.

The string exception is deprecated but is simpler for this example.



# DeprecationWarning: raising a string exception is deprecated

def Goto_is_not_dead(nIn):
try:
if (nIn == 1): raise 'Goto_Exit'
if (nIn == 2): raise 'Goto_Exit'

print 'Good Input ' + str(nIn)
raise 'Goto_Exit'

except 'Goto_Exit':
print 'any input ' + str(nIn)

if __name__ == '__main__':
Goto_is_not_dead(2)
Goto_is_not_dead(3)
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      08-17-2008
On Sat, 16 Aug 2008 23:20:52 +0200, Kurien Mathew wrote:

> Any suggestions on a good python equivalent for the following C code:
>
> while (loopCondition)
> {
> if (condition1)
> goto next;
> if (condition2)
> goto next;
> if (condition3)
> goto next;
> stmt1;
> stmt2;
> next:
> stmt3;
> stmt4;
> }


(Don't) use the `goto` module: http://entrian.com/goto/

from goto import goto, label

# ...

while loop_condition:
if condition1 or condition2 or condition3:
goto .next
print 'stmt1'
print 'stmt2'
label .next
print 'stmt3'
print 'stmt4'


Ciao,
Marc 'BlackJack' Rintsch
 
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
question about a command like 'goto ' in Python's bytecode or it'sjust a compiler optimization? higer Python 8 06-19-2009 12:11 AM
ANNOUNCE: 'goto' for Python Richie Hindle Python 20 04-04-2004 10:45 PM
Re: ANNOUNCE: 'goto' for Python Richie Hindle Python 1 04-01-2004 11:16 PM
Re: ANNOUNCE: 'goto' for Python Camilo Olarte Python 3 04-01-2004 10:22 PM
Working around a lack of 'goto' in python Brett Python 13 03-07-2004 08:49 PM



Advertisments