Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to GOTO in conditions, Python

Since there is no goto operator in Python, what technique can be used instead?

Condition If it is true, go to thread 1, if is false, go to thread 2 In thread we do something small and after that we go to thread 2 where all other actions take place.

like image 859
Maks Avatar asked Dec 14 '10 11:12

Maks


2 Answers

Since there is no goto operator in Python, what technique can be used instead?

Constructing your code logically and semantically.

if condition:
    perform_some_action()

perform_other_actions()
like image 181
detly Avatar answered Sep 29 '22 21:09

detly


def thread_1():
  # Do thread_1 type stuff here.

def thread_2():
  # Do thread_2 type stuff here.

if condition:
    thread_1()

# If condition was false, just run thread_2(). 
# If it was true then thread_1() will return to this point.
thread_2()

edit: I'm assuming that by "thread" you mean a piece of code (otherwise known as a subroutine or a function). If you're talking about threads as in parallel execution then you'll need more detail in the question.

like image 25
Optimal Cynic Avatar answered Sep 29 '22 23:09

Optimal Cynic