Exiting from python Command Line
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
To exit from Python command line, I have to type exit(). If I type exit, it says
Use exit() or Ctrl-Z plus Return to exit
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.
python
add a comment |
To exit from Python command line, I have to type exit(). If I type exit, it says
Use exit() or Ctrl-Z plus Return to exit
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.
python
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...
– summea
Mar 16 '12 at 0:59
right 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.
– Ank
Mar 16 '12 at 1:00
1
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12
add a comment |
To exit from Python command line, I have to type exit(). If I type exit, it says
Use exit() or Ctrl-Z plus Return to exit
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.
python
To exit from Python command line, I have to type exit(). If I type exit, it says
Use exit() or Ctrl-Z plus Return to exit
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.
python
python
asked Mar 16 '12 at 0:57
AnkAnk
2,245184586
2,245184586
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...
– summea
Mar 16 '12 at 0:59
right 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.
– Ank
Mar 16 '12 at 1:00
1
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12
add a comment |
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...
– summea
Mar 16 '12 at 0:59
right 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.
– Ank
Mar 16 '12 at 1:00
1
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12
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...– summea
Mar 16 '12 at 0:59
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...– summea
Mar 16 '12 at 0:59
right 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.
– Ank
Mar 16 '12 at 1:00
right 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.
– Ank
Mar 16 '12 at 1:00
1
1
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12
add a comment |
11 Answers
11
active
oldest
votes
In 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
.
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
add a comment |
This works for me, best way to come out of python prompt.
exit()
add a comment |
When 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:
<function exit at 0x00B97FB0>
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?)
4
In this case, the real exit function is not even in scope unless youimport sys
, after which you would call sys.exit() to exit the interpreter.
– Michael Dillon
Mar 16 '12 at 3:18
add a comment |
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
add a comment |
This message is the __str__
attribute of exit
look at these examples :
1
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
2
>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
3
>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
add a comment |
Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.
The syntax of Python is such that exit
, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).
add a comment |
With Anaconda 4.5+ and Python 3.6+ on Windows use
Ctrl+Z
or
exit()
In some cases, you might have to use
Ctrl+Break
If your computer doesn't have Break
key then see here.
add a comment |
You can fix that.
Link PYTHONSTARTUP
to a python file with the following
# Make exit work as expected
type(exit).__repr__ = type(exit).__call__
How does this work?
The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.
When you type exit()
it evaluates to a callable object of type site.Quitter
and calls its __call__
function which exits the system. When you type exit
it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__
on the object.
We can take advantage of this by linking __repr__
to __call__
and thus get the expected behavior of exiting the sustem even when we type exit
without parentheses.
2
They took the effort to makeexit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
– Przemek D
May 8 '18 at 7:54
add a comment |
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
add a comment |
To exit from Python terminal, simply just do:
exit()
Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...
or as a shortcut, press:
Ctrl + D
on your keyboard...
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
add a comment |
"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.
Except if you assign toexit
, you override this symbol, andexit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
– Przemek D
Nov 6 '18 at 12:38
add a comment |
protected by Josh Crozier Oct 19 '18 at 18:30
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
In 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
.
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
add a comment |
In 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
.
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
add a comment |
In 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
.
In 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
.
edited Mar 16 '12 at 1:16
answered Mar 16 '12 at 1:03
DunesDunes
24.9k44869
24.9k44869
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
add a comment |
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
1
1
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
That was from Active Python.
– Ank
Mar 16 '12 at 1:04
6
6
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
Ctrl-Z is what you do on Windows (well, DOS) where you would do Ctrl-D on Unix-like systems.
– Karl Knechtel
Mar 16 '12 at 1:10
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
@KarlKnechtel Ah, good to know. I have little experience of programming on windows.
– Dunes
Mar 16 '12 at 1:15
add a comment |
This works for me, best way to come out of python prompt.
exit()
add a comment |
This works for me, best way to come out of python prompt.
exit()
add a comment |
This works for me, best way to come out of python prompt.
exit()
This works for me, best way to come out of python prompt.
exit()
answered Feb 29 '16 at 8:58
Sreedhar GSSreedhar GS
1,85311622
1,85311622
add a comment |
add a comment |
When 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:
<function exit at 0x00B97FB0>
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?)
4
In this case, the real exit function is not even in scope unless youimport sys
, after which you would call sys.exit() to exit the interpreter.
– Michael Dillon
Mar 16 '12 at 3:18
add a comment |
When 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:
<function exit at 0x00B97FB0>
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?)
4
In this case, the real exit function is not even in scope unless youimport sys
, after which you would call sys.exit() to exit the interpreter.
– Michael Dillon
Mar 16 '12 at 3:18
add a comment |
When 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:
<function exit at 0x00B97FB0>
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?)
When 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:
<function exit at 0x00B97FB0>
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?)
answered Mar 16 '12 at 1:10
mgibsonbrmgibsonbr
18.9k75288
18.9k75288
4
In this case, the real exit function is not even in scope unless youimport sys
, after which you would call sys.exit() to exit the interpreter.
– Michael Dillon
Mar 16 '12 at 3:18
add a comment |
4
In this case, the real exit function is not even in scope unless youimport sys
, after which you would call sys.exit() to exit the interpreter.
– Michael Dillon
Mar 16 '12 at 3:18
4
4
In this case, the real exit function is not even in scope unless you
import sys
, after which you would call sys.exit() to exit the interpreter.– Michael Dillon
Mar 16 '12 at 3:18
In this case, the real exit function is not even in scope unless you
import sys
, after which you would call sys.exit() to exit the interpreter.– Michael Dillon
Mar 16 '12 at 3:18
add a comment |
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
add a comment |
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
add a comment |
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.
answered Mar 16 '12 at 1:00
Jay SlupeskyJay Slupesky
1,7791114
1,7791114
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
add a comment |
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Only way that worked for me...
– Mike Wise
Nov 20 '15 at 13:18
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
Seems like this method doesn't work if the script ran into an error.
– blaylockbk
Dec 6 '16 at 20:55
add a comment |
This message is the __str__
attribute of exit
look at these examples :
1
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
2
>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
3
>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
add a comment |
This message is the __str__
attribute of exit
look at these examples :
1
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
2
>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
3
>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
add a comment |
This message is the __str__
attribute of exit
look at these examples :
1
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
2
>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
3
>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
This message is the __str__
attribute of exit
look at these examples :
1
>>> print exit
Use exit() or Ctrl-D (i.e. EOF) to exit
2
>>> exit.__str__()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
3
>>> getattr(exit, '__str__')()
'Use exit() or Ctrl-D (i.e. EOF) to exit'
answered Mar 16 '12 at 1:05
moulmoul
468210
468210
add a comment |
add a comment |
Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.
The syntax of Python is such that exit
, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).
add a comment |
Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.
The syntax of Python is such that exit
, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).
add a comment |
Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.
The syntax of Python is such that exit
, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).
Because the interpreter is not a shell where you provide commands, it's - well - an interpreter. The things that you give to it are Python code.
The syntax of Python is such that exit
, by itself, cannot possibly be anything other than a name for an object. Simply referring to an object can't actually do anything (except what the read-eval-print loop normally does; i.e. display a string representation of the object).
answered Mar 16 '12 at 1:13
Karl KnechtelKarl Knechtel
37.7k56294
37.7k56294
add a comment |
add a comment |
With Anaconda 4.5+ and Python 3.6+ on Windows use
Ctrl+Z
or
exit()
In some cases, you might have to use
Ctrl+Break
If your computer doesn't have Break
key then see here.
add a comment |
With Anaconda 4.5+ and Python 3.6+ on Windows use
Ctrl+Z
or
exit()
In some cases, you might have to use
Ctrl+Break
If your computer doesn't have Break
key then see here.
add a comment |
With Anaconda 4.5+ and Python 3.6+ on Windows use
Ctrl+Z
or
exit()
In some cases, you might have to use
Ctrl+Break
If your computer doesn't have Break
key then see here.
With Anaconda 4.5+ and Python 3.6+ on Windows use
Ctrl+Z
or
exit()
In some cases, you might have to use
Ctrl+Break
If your computer doesn't have Break
key then see here.
edited Nov 16 '18 at 11:41
answered Oct 13 '18 at 1:44
Shital ShahShital Shah
26k511098
26k511098
add a comment |
add a comment |
You can fix that.
Link PYTHONSTARTUP
to a python file with the following
# Make exit work as expected
type(exit).__repr__ = type(exit).__call__
How does this work?
The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.
When you type exit()
it evaluates to a callable object of type site.Quitter
and calls its __call__
function which exits the system. When you type exit
it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__
on the object.
We can take advantage of this by linking __repr__
to __call__
and thus get the expected behavior of exiting the sustem even when we type exit
without parentheses.
2
They took the effort to makeexit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
– Przemek D
May 8 '18 at 7:54
add a comment |
You can fix that.
Link PYTHONSTARTUP
to a python file with the following
# Make exit work as expected
type(exit).__repr__ = type(exit).__call__
How does this work?
The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.
When you type exit()
it evaluates to a callable object of type site.Quitter
and calls its __call__
function which exits the system. When you type exit
it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__
on the object.
We can take advantage of this by linking __repr__
to __call__
and thus get the expected behavior of exiting the sustem even when we type exit
without parentheses.
2
They took the effort to makeexit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
– Przemek D
May 8 '18 at 7:54
add a comment |
You can fix that.
Link PYTHONSTARTUP
to a python file with the following
# Make exit work as expected
type(exit).__repr__ = type(exit).__call__
How does this work?
The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.
When you type exit()
it evaluates to a callable object of type site.Quitter
and calls its __call__
function which exits the system. When you type exit
it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__
on the object.
We can take advantage of this by linking __repr__
to __call__
and thus get the expected behavior of exiting the sustem even when we type exit
without parentheses.
You can fix that.
Link PYTHONSTARTUP
to a python file with the following
# Make exit work as expected
type(exit).__repr__ = type(exit).__call__
How does this work?
The python command line is a read-evaluate-print-loop, that is when you type text it will read that text, evaluate it, and eventually print the result.
When you type exit()
it evaluates to a callable object of type site.Quitter
and calls its __call__
function which exits the system. When you type exit
it evaluates to the same callable object, without calling it the object is printed which in turn calls __repr__
on the object.
We can take advantage of this by linking __repr__
to __call__
and thus get the expected behavior of exiting the sustem even when we type exit
without parentheses.
edited May 11 '18 at 8:48
answered Mar 20 '18 at 9:17
akuhnakuhn
23.2k25777
23.2k25777
2
They took the effort to makeexit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
– Przemek D
May 8 '18 at 7:54
add a comment |
2
They took the effort to makeexit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.
– Przemek D
May 8 '18 at 7:54
2
2
They took the effort to make
exit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.– Przemek D
May 8 '18 at 7:54
They took the effort to make
exit
print the "helpful" string, but they couldn't just bind it to do the natural thing and exit the interpreter. Being forced to type the parentheses every time is frustrating. This answer is perfect and should be the accepted one instead.– Przemek D
May 8 '18 at 7:54
add a comment |
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
add a comment |
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
add a comment |
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
If you stuck in python command line and none of above solutions worked for you,
try exit(2)
answered Aug 15 '18 at 19:35
Mahdi BaghbaniMahdi Baghbani
185
185
add a comment |
add a comment |
To exit from Python terminal, simply just do:
exit()
Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...
or as a shortcut, press:
Ctrl + D
on your keyboard...
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
add a comment |
To exit from Python terminal, simply just do:
exit()
Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...
or as a shortcut, press:
Ctrl + D
on your keyboard...
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
add a comment |
To exit from Python terminal, simply just do:
exit()
Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...
or as a shortcut, press:
Ctrl + D
on your keyboard...
To exit from Python terminal, simply just do:
exit()
Please pay attention it's a function which called as most user mix it with exit without calling, but new Pyhton terminal show a message...
or as a shortcut, press:
Ctrl + D
on your keyboard...
edited Nov 6 '18 at 12:54
answered Aug 18 '18 at 1:44
AlirezaAlireza
52.4k14178124
52.4k14178124
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
add a comment |
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
1
1
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
Everything you said has been covered in the answers above already.
– Przemek D
Nov 6 '18 at 12:41
add a comment |
"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.
Except if you assign toexit
, you override this symbol, andexit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
– Przemek D
Nov 6 '18 at 12:38
add a comment |
"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.
Except if you assign toexit
, you override this symbol, andexit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
– Przemek D
Nov 6 '18 at 12:38
add a comment |
"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.
"exit" is a valid variable name that can be used in your Python program. You wouldn't want to exit the interpreter when you're just trying to see the value of that variable.
answered Aug 15 '18 at 20:07
Chad KennedyChad Kennedy
1,202614
1,202614
Except if you assign toexit
, you override this symbol, andexit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
– Przemek D
Nov 6 '18 at 12:38
add a comment |
Except if you assign toexit
, you override this symbol, andexit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.
– Przemek D
Nov 6 '18 at 12:38
Except if you assign to
exit
, you override this symbol, and exit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.– Przemek D
Nov 6 '18 at 12:38
Except if you assign to
exit
, you override this symbol, and exit()
will no longer work. Although this symbol is a valid variable name, using it in such a way doesn't seem to be a very good practice.– Przemek D
Nov 6 '18 at 12:38
add a comment |
protected by Josh Crozier Oct 19 '18 at 18:30
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
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...– summea
Mar 16 '12 at 0:59
right 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.
– Ank
Mar 16 '12 at 1:00
1
exit or exit() throws an error for me about 20% of installations I've found in the world... Only CTRL+Z + return consistently works.
– Paulo Carvalho
Jan 28 at 13:12