简体   繁体   English

Python 的交互模式是如何工作的?

[英]How does Python's interactive mode work?

I want to know how Python interactive mode works.我想知道 Python 交互模式是如何工作的。 Usually when you run Python script on CPython it will go trough the process of lexical analysis, parsing, gets compiled into .pyc file, and finally the .pyc file is interpreted.通常当你在 CPython 上运行 Python 脚本时,它会经历词法分析、解析、编译成.pyc文件,最后解释.pyc文件的过程。

Does this 4-step process happen while using interactive mode also, r is there a more efficient way of implementing?这个 4 步过程是否也发生在使用交互模式时,有没有更有效的实现方式?

Python has two basic modes: normal and interactive. Python 有两种基本模式:普通模式和交互模式。 The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter.正常模式是脚本化和完成的.py文件在 Python 解释器中运行的模式。 Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory.交互模式是一个命令行 shell,它为每个语句提供即时反馈,同时在活动内存中运行先前提供的语句。 As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.当新的行被输入解释器时,输入的程序会被部分和全部评估。
The same occurs with the .cpy files. .cpy文件也会发生同样的情况。 Interactive mode basically doing the entire process for each line.交互模式基本上为每一行做整个过程。 I highly doubt that there's a more efficient way to do so.我非常怀疑是否有更有效的方法来做到这一点。
The iPython notebook works in a similar way. iPython 笔记本以类似的方式工作。

From the article Is Python interpreted or compiled?来自文章Python 是解释的还是编译的? Yes. 是的。

Another important Python feature is its interactive prompt. Python 的另一个重要特性是它的交互式提示。 You can type Python statements and have them immediately executed.您可以键入 Python 语句并立即执行它们。 This interactivity is usually missing in "compiled" languages, but even at the Python interactive prompt, your Python is compiled to bytecode, and then the bytecode is executed.这种交互性在“编译”语言中通常是缺失的,但即使在 Python 交互提示下,你的 Python 也被编译为字节码,然后字节码被执行。 This immediate execution, and Python' lack of an explicit compile step, are why people call the Python executable, "the Python interpreter."这种立即执行以及 Python 缺乏明确的编译步骤,就是人们将 Python 可执行文件称为“Python 解释器”的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM