Python Glossary Русский

all Python 3Built-in typesterm

quit

special values

The twin of exit from the site module: quit() raises SystemExit and is meant for the interactive shell. Programs may not have it, so use sys.exit().

Syntax

quit(code=None)

quit and exit are two distinct objects of the same Quitter class and behave the same: a call closes sys.stdin and raises SystemExit, with code None by default, which means success. Both exist only thanks to the site module, so under python -S or in an embedded interpreter quit() is a NameError. In scripts and exercise solutions, finish with sys.exit() or simply reach the end of the file.

Examples

print(type(quit) is type(exit), quit is exit)   # → True False
try:
    quit()   # → SystemExit
except SystemExit as e:
    print(e.code)   # → None — код успеха
import subprocess
import sys
r = subprocess.run([sys.executable, '-S', '-c', 'quit()'],
                   capture_output=True, text=True)
print(r.returncode, 'NameError' in r.stderr)   # → 1 True

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy