y33t
y33t

Reputation: 679

Proper way to quit a function in Python

Assume I have the following ;

def test():
    while 1:
        a = b
        time.sleep(60)
        c = b
        if(c==a):
            do something
            then quit the function

What is the proper way to quit from a function having this structure ?

Upvotes: 0

Views: 170

Answers (3)

Levon
Levon

Reputation: 143047

You could just use a return statement.

That would be the most direct way, by just placing the return where you want to quit ("then quit the function").

  if(c==a):
     do something
     return 

You could also use this to return any results you have to the calling code.

Eg., return some_results

Python doc for return

Upvotes: 5

Christian Witts
Christian Witts

Reputation: 11585

Just use the return statement to exit the function call.

def blah():
    return  # Returns None if nothing passed back with it
def blah():
    return some_value, some_value2 # Return some values with it if you want.

Upvotes: 1

Rodrigo Queiro
Rodrigo Queiro

Reputation: 1336

Use the return statement: eg

def test():
    while 1:
        a = b
        time.sleep(60)
        c = b
        if c == a:
            print a
            return

break would also work, by leaving the while loop.

Upvotes: 2

Related Questions