Search code examples
pythonfunctionquit

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 ?


Solution

  • 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