Exit () and Quit () Function in Python 3

Exit and Quit both are used to get out of a program which is running.

he exit and quit "functions" are actually site.Quitter objects and are loaded, at interpreter start up, from site.py. However, if the interpreter is started with the -S flag, or a custom site.py is used then exit and quit may not be present.
Recommendation
Replace uses of exit() and quit() with sys.exit() which is built into the interpreter and is guaranteed to be present.
Example
In this example, exit() is used and will fail if the interpreter is passed the -S option.
1
2
3
4
5
6
def main():
    try:
        process()
    except Exception as ex:
        print(ex)
        exit(1)
In this example, sys.exit() is used and will behave the same regardless of the interpreter options.

1
2
3
4
5
6
7
8
import sys
def main():
    try:
        process()
    except Exception as ex:
        print(ex)
        sys.exit(1)

Comments

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me