osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is the Difference Between quit() and exit() commands in Python?


Hongyi Zhao wrote:

> What is the Difference Between quit() and exit() commands in Python?

They are instances of the same type

>>> import inspect
>>> type(quit) is type(exit)
True
>>> print(inspect.getsource(type(quit)))
class Quitter(object):
    def __init__(self, name, eof):
        self.name = name
        self.eof = eof
    def __repr__(self):
        return 'Use %s() or %s to exit' % (self.name, self.eof)
    def __call__(self, code=None):
        # Shells like IDLE catch the SystemExit, but listen when their
        # stdin wrapper is closed.
        try:
            sys.stdin.close()
        except:
            pass
        raise SystemExit(code)


There is no difference, except for the name attribute and the repr() text:

>>> exit.name, exit.eof, exit
('exit', 'Ctrl-D (i.e. EOF)', Use exit() or Ctrl-D (i.e. EOF) to exit)
>>> quit.name, quit.eof, quit
('quit', 'Ctrl-D (i.e. EOF)', Use quit() or Ctrl-D (i.e. EOF) to exit)