TheLastGIS
TheLastGIS

Reputation: 434

sys.exit(): is there a less extreme alternative?

I was hoping to get some kind of advice regarding the use of sys.exit(). I have used sys.exit() to stop a script from running any further:

My code:

if  x != 20:
    query = raw_input("X does not equal 20. Do you want to continue? (Y/N)")
    if query in ['y', 'Y']:
        pass
    else:
        sys.exit()

I have done some searching (and am still searching) but I was hoping to gain some clarity regarding the best practices use of sys.exit(). Is there something a little less extreme than killing the script? I'm considering the combination of extra loop and a more inquisitive question.

Upvotes: 2

Views: 4370

Answers (3)

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73608

Since this is used at the beginning of your script (as mentioned by you). Use the coding pattern written below. And use return instead of sys.exit (actually since you are exiting from the script itself thereby terminating the process altogether sys.exit is not a bad practice).

Whether you use return or sys.exit return appropriate integer. its a good practice.

def do_something():
    //process something
    return 1

if __name__ == '__main__':
    query = raw_input("Do you want to continue? (Y/N)")
    if query.lower() == 'y':
        //do something
        do_something()
    else:
        print 'ERROR: Cant Understand Input. It has to be (Y/N). Exiting...'
        return 0

Upvotes: 4

theodox
theodox

Reputation: 12208

As other commenters have noted, a function provides you the option of using return to bail out from anywhere in the script and that's probably the best choice - I just thought I'd mention one piece of trivia:

Technically sys.exit raises a SystemExit exception. You can catch a SystemExit with an ordinary try-catch, effectively 'canceling' the exit if you want to. This might come in handy if you have more complex code in which a low level function might not have enough context to make a good decision but higher level code may want to step in and clean up.

People from C/C++ and lots of other languages get hives from this tactic but it's not uncommon in Pythonland. It's not a good idea to use exceptions for flow control in general but if you are using other people's functions, or combining smaller scripts into a larger application, it may be useful.

Don't forget, btw, that if control falls through to the bottom of your script it will just return 'None' anyway, so in your example:

if  x != 20:
    query = raw_input("X does not equal 20. Do you want to continue? (Y/N)")
    if query in ['y', 'Y']:
        // do something

is the same as

if  x != 20:
    query = raw_input("X does not equal 20. Do you want to continue? (Y/N)")
    if query in ['y', 'Y']:
        // do something
    else:
        sys.exit()

EXCEPT for the fact that you could try/catch around the second but not the first... though I assume your example is snipped from a larger sample where return is the better escape route

Upvotes: 3

gukoff
gukoff

Reputation: 2240

Place your code in the main() function:

def main():
    // your code

if __name__ == '__main__':
    main()

Then you can exit from your script just by return

Upvotes: 4

Related Questions