从python命令行退出

问题:从python命令行退出

要从Python命令行退出,我必须键入exit()。如果我输入exit,它会说

Use exit() or Ctrl-Z plus Return to exit

通常,当您键入时exit,您想退出该程序。当解释程序知道我要退出命令行时,为什么会给我上述错误?为什么不退出呢?我知道这并不重要,这是一个愚蠢的问题,但我很好奇。

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.


回答 0

在我的python解释器exit中,实际上是一个字符串而不是一个函数- 'Use Ctrl-D (i.e. EOF) to exit.'。您可以输入以下内容来检查口译员type(exit)

在活动的python中,正在发生的事情是exit是一个函数。如果不调用该函数,它将打印出对象的字符串表示形式。这是返回的任何对象的默认行为。只是设计人员认为人们可能会尝试键入exit退出解释器,因此他们使exit函数的字符串表示形式很有帮助。您可以通过键入str(exit)甚至来检查此行为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.


回答 1

这对我有效,这是从python提示符中提取出来的最好方法。

出口()

This works for me, best way to come out of python prompt.

exit()


回答 2

exit在命令行中键入时,它将查找具有该名称的变量并在其上调用__repr__(或__str__)。通常,您会得到如下结果:

<function exit at 0x00B97FB0>

但是他们决定为该exit对象重新定义该功能,以显示一条有用的消息。这是否是一个愚蠢的行为,是一个主观的问题,但它不“只是退出”的一个可能原因是:

例如,假设您正在调试器中查看一些代码,并且其中一个对象引用了该exit函数。当调试器尝试调用__repr__该对象以向您显示该功能时,该程序突然停止!这真的是出乎意料的,并且采取应对措施可能会使事情进一步复杂化(例如,即使将行为限制为命令行,如果尝试打印具有exit属性的对象该怎么办?)

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?)


回答 3

这个消息是__str__的属性exit

看这些例子:

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'

回答 4

我建议您使用退出Python解释器Ctrl-D。这是文件结束或传输结束的旧ASCII码。

I recommend you exit the Python interpreter with Ctrl-D. This is the old ASCII code for end-of-file or end-of-transmission.


回答 5

在Windows上与Anaconda 4.5+和Python 3.6+一起使用

Ctrl+Z

要么

exit()

在某些情况下,您可能必须使用

Ctrl+Break

如果您的计算机没有Break钥匙,请参见此处

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.


回答 6

因为解释器不是您提供命令的外壳,所以它是-解释器。您提供的是Python代码。

Python的语法exit本身不可能是对象的名称。简单地引用一个对象实际上并不能做任何事情(read-eval-print循环通常会执行的操作除外;即显示该对象的字符串表示形式)。

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).


回答 7

您可以解决该问题。

PYTHONSTARTUP使用以下链接到python文件

# Make exit work as expected
type(exit).__repr__ = type(exit).__call__

这是如何运作的?

python命令行是一个read-evaluate-print-loop,也就是说,当您键入文本时,它将读取该文本,对其进行评估并最终打印结果。

键入时,exit()它将求值为类型的可调用对象,site.Quitter并调用其__call__退出系统的函数。键入时,exit它求值到相同的可调用对象,而不调用它,则将打印该对象,从而依次调用__repr__该对象。

我们可以通过链接__repr__来利用这一点__call__,从而获得预期的退出系统的行为,即使我们键入时exit不带括号也是如此。

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 system even when we type exit without parentheses.


回答 8

要退出Python终端,只需执行以下操作:

exit()

请注意,这是一个函数,大多数用户在不调用它的情况下将其与退出混合使用,但是新的Pyhton终端会显示一条消息…

或作为快捷方式,按:

Ctrl + D

在键盘上…

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…


回答 9

如果您陷在python命令行中,但以上解决方案均不适合您,请尝试 exit(2)

If you stuck in python command line and none of above solutions worked for you, try exit(2)


回答 10

“ exit”是可以在Python程序中使用的有效变量名。当您尝试查看该变量的值时,您不想退出解释器。

“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.