31

Would anybody tell me what is the difference between builtin function exit() and quit().

Please correct me if I am wrong at any point. I have tried to check it but I am not getting anything.

1) When I use help() and type() function for each one, it says that both are object of class Quitter, which is defined in the module site.

2) When I use id() to check the addresses of each one, it returns different addresses i.e. these are two different objects of same class site.Quitter.

>>> id(exit)
13448048
>>> id(quit)
13447984

3) And since the addresses remains constant over the subsequent calls, i.e. it is not using return wrapper each time.

>>> id(exit)
13448048
>>> id(quit)
13447984

Would anybody provide me details about the differences between these two and if both are doing the same thing, why we need two different functions.

5
  • 3
    The documentation strongly implies they're the same: docs.python.org/2/library/… Commented Oct 10, 2013 at 6:40
  • 1
    Then why do we need two different functions, which are same and why these are return different ids.. Commented Oct 10, 2013 at 6:43
  • 3
    You're assuming there's a reason. My guess is the explanation is prosaic. There's both names because in the interpreter it's more user-friendly since you don't have to remember which name it is. And they're different ids because someone coded it that way and didn't think it makes a difference whether it's one or two. Commented Oct 10, 2013 at 6:45
  • 5
    This looks to be in contradiction with the Zen: "There should be only one way..." Commented Apr 24, 2014 at 8:44
  • 1
    In PHP, die and exit are also synonyms. There, I recommend exit when the exit is planned, and die when it’s the result of an error. In Python, you can do the same thing: exit when it’s planned and quit when there’s an error. Commented Jan 26, 2022 at 0:10

1 Answer 1

28

The short answer is: both exit() and quit() are instances of the same Quitter class, the difference is in naming only, that must be added to increase user-friendliness of the interpreter.

For more details let's check out the source: http://hg.python.org/cpython

In Lib/site.py (python-2.7) we see the following:

def setquit():
    """Define new builtins 'quit' and 'exit'.

    These are objects which make the interpreter exit when called.
    The repr of each object contains a hint at how it works.

    """
    if os.sep == ':':
        eof = 'Cmd-Q'
    elif os.sep == '\\':
        eof = 'Ctrl-Z plus Return'
    else:
        eof = 'Ctrl-D (i.e. EOF)'

    class Quitter(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
            return 'Use %s() or %s to exit' % (self.name, 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)
    __builtin__.quit = Quitter('quit')
    __builtin__.exit = Quitter('exit')

The same logic we see in python-3.x.

Sign up to request clarification or add additional context in comments.

5 Comments

Use exit if it's planned, quit if it's not.
@Joao-3 Any logic for this "Use exit() if it's planned, quit() if it's not"?
I'd use sys.exit in scripts to get more flexibility (to be able to set an exit code and text) and to handle errors or successes more explicily. Or even just raised exceptions so that in case of import one could easyly handle those
@AkshayKatiha No logic, really. “quit” just sounds more drastic than “exit”, so it feels like something you would do in response to a disaster. The point is that you’ve two names for the same thing, so you may as well use the names to distinguish between the situations.
@Manngo: This explanation makes sense and There should be one-- and preferably only one --obvious way to do it Quit the job :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.