Usually when you type exit , you would want to exit the program. Why does the interpreter give me the above error when it knows I am trying to exit the command line? Why doesn't it just exit? I know it doesn't matter and its a silly question but I am curious.
It might have to do with how functions are often called with () at the end. otherwise, it might (possibly) be a variable. or some sort of object.
Commented Mar 16, 2012 at 0:59right but the interpreter knows that i'm trying to exit and thats why prints that message. otherwise it would have printed an error message. If it knows i'm trying to exit, it can just exit.
Commented Mar 16, 2012 at 1:00exit or exit() throws an error for me about 20% of installations I've found in the world. Only CTRL+Z + return consistently works.
Commented Jan 28, 2019 at 13:12I think exit doesn't work with all python versions ( Ctrl-Z on windows, Ctrl-D on Linux and exit() work with all python versions
Commented Feb 10, 2021 at 9:08I installed python on windows and ran into the same issue. "exit" does not work and even did not give me such hint. I also tried quit and [Ctrl][d] but nothing worked. Not even exit() or quit() is working. Hitting [Ctrl][z] followed by a [Return] did the job. However, with such a lousy UX I wonder why people actually end up using python - what a nightmare. All the answers trying to give arguments for this ugly behavior simply miss the point of UX.
Commented Jun 9, 2022 at 16:19This works for me, best way to come out of python prompt.
exit()
54.9k 7 7 gold badges 108 108 silver badges 145 145 bronze badges
answered Feb 29, 2016 at 8:58
Sreedhar GS Sreedhar GS
2,760 1 1 gold badge 25 25 silver badges 26 26 bronze badges
I'm confused about this answer. the question itself says they already know that exit() works, and they want to know why exit without parens does not work. So this answer just repeats part of the question and doesn't actually answer it.
Commented Sep 9, 2019 at 14:47I kind of want to downvote this answer for not reading the question, but I suppose for most python installations this is correct; it's just that the user said this doesn't work in their case, so recommending it seems. improper.
Commented Jan 19, 2020 at 21:31@kettlecrab who cares about the specifics of the original question, that is for one specific person. many of us type in related keywords and end up in threads like these where these kind of answers help us.
Commented Feb 23 at 11:12In my python interpreter exit is actually a string and not a function -- 'Use Ctrl-D (i.e. EOF) to exit.' . You can check on your interpreter by entering type(exit)
In active python what is happening is that exit is a function. If you do not call the function it will print out the string representation of the object. This is the default behaviour for any object returned. It's just that the designers thought people might try to type exit to exit the interpreter, so they made the string representation of the exit function a helpful message. You can check this behaviour by typing str(exit) or even print exit .
answered Mar 16, 2012 at 1:03 39.5k 7 7 gold badges 84 84 silver badges 102 102 bronze badges Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems. Commented Mar 16, 2012 at 1:10 @KarlKnechtel Ah, good to know. I have little experience of programming on windows. Commented Mar 16, 2012 at 1:15When you type exit in the command line, it finds the variable with that name and calls __repr__ (or __str__ ) on it. Usually, you'd get a result like:
But they decided to redefine that function for the exit object to display a helpful message instead. Whether or not that's a stupid behavior or not, is a subjective question, but one possible reason why it doesn't "just exit" is:
Suppose you're looking at some code in a debugger, for instance, and one of the objects references the exit function. When the debugger tries to call __repr__ on that object to display that function to you, the program suddenly stops! That would be really unexpected, and the measures to counter that might complicate things further (for instance, even if you limit that behavior to the command line, what if you try to print some object that have exit as an attribute?)