Python 是否被解释(如 Javascript 或 PHP)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/745743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:47:15  来源:igfitidea点击:

Is Python interpreted (like Javascript or PHP)?

python

提问by Geuis

Is Python strictly interpreted at run time, or can it be used to develop programs that run as background applications (like a Java app or C program)?

Python 是在运行时严格解释的,还是可以用来开发作为后台应用程序运行的程序(如 Java 应用程序或 C 程序)?

回答by Brian

As the varied responses will tell you, the line between interpreted and compiled is no longer as clear as it was when such terms were coined. In fact, it's also something of a mistake to consider languagesas being either interpreted or compiled, as different implementationsof languages may do different things. These days you can find both C interpretersand Javascript compilers.

正如不同的回答会告诉你的那样,解释和编译之间的界限不再像创造这些术语时那样清晰。事实上,将语言视为解释型或编译型也是错误的,因为不同的语言实现可能会做不同的事情。现在,您可以找到C 解释器Javascript 编译器

Even when looking at an implementation, things still aren't clear-cut. There are layers of interpretation. Here are a few of the gradations between interpreted and compiled:

即使在查看实现时,事情仍然不明确。有层层解释。以下是解释和编译之间的一些渐变:

  1. Pure interpretation. Pretty much what it says on the tin. Read a line of source and immediately do what it says. This isn't actually done by many production languages - pretty much just things like shell scripts.

  2. Tokenisation+ interpretation. A trivial optimisation on the above. Rather than interpret each line from scratch, it's first tokenised (that is, rather than seeing a string like "print 52 + x", it's translated into a stream of tokens (eg. [PRINT_STATEMENT, INTEGER(52), PLUS_SIGN, IDENTIFIER('x')]) to avoid repeatedly performing that state of interpretation. Many versions of basic worked this way.

  3. Bytecodecompilation. This is the approach taken by languages like Java and C# (though see below). The code is transformed into instructions for a "virtual machine". These instructions are then interpreted. This is also the approach taken by python (or at least cpython, the most common implementation.) The Jythonand Ironpythonimplementations also take this approach, but compile to the bytecode for the Java and C# virtual machines resepectively.

  4. Bytecode + Just in Time compilation. As above, but rather than interpreting the bytecodes, the code that would be performed is compiled from the bytecode at the point of execution, and then run. In some cases, this can actually outperform native compilation, as it is free to perform runtime analysis on the code, and can use specific features of the current processor (while static compilation may need to compile for a lowest common denominator CPU). Later versions of Java, and C# use this approach. Psycoperforms this for python.

  5. Native machine-code compilation. The code is compiled to the machine code of the target system. You may think we've now completely eliminated interpretation, but even here there are subtleties. Some machine code instructions are not actually directly implemented in hardware, but are in fact implemented via microcode- even machine code is sometimes interpreted!

  1. 纯粹的诠释。罐头上写的差不多。阅读一行源代码并立即按照它说的去做。这实际上并不是由许多生产语言完成的 - 几乎只是像 shell 脚本这样的东西。

  2. 标记化+ 解释。对上面的一个微不足道的优化。不是从头开始解释每一行,而是首先对其进行标记化(即,不是看到像“print 52 + x”这样的字符串,而是将其翻译成标记流(例如[PRINT_STATEMENT, INTEGER(52), PLUS_SIGN, IDENTIFIER('x')])以避免重复执行该解释状态。许多版本基本就是这样工作的。

  3. 字节码编译。这是 Java 和 C# 等语言所采用的方法(尽管见下文)。代码被转换为“虚拟机”的指令。然后解释这些指令。这也是蟒蛇采取的方法(或至少CPython中,最常见的实现)的Jython的IronPython的实现也采取这种方法,而是编译成字节码的Java和C#的虚拟机resepectively。

  4. 字节码 +及时编译。如上所述,不是解释字节码,而是从执行点的字节码编译将要执行的代码,然后运行。在某些情况下,这实际上可以胜过本地编译,因为它可以自由地对代码执行运行时分析,并且可以使用当前处理器的特定功能(而静态编译可能需要针对最低公分母 CPU 进行编译)。更高版本的 Java 和 C# 使用这种方法。 Psyco为 python 执行此操作。

  5. 本机机器代码编译。代码被编译为目标系统的机器码。你可能认为我们现在已经完全消除了解释,但即使在这里也有微妙之处。一些机器码指令实际上并不是直接在硬件中实现的,而是通过微码实现的——甚至有时会解释机器码!

回答by John Millikin

There's multiple questions here:

这里有多个问题:

  1. No, Python is not interpreted. The standard implementation compiles to bytecode, and then executes in a virtual machine. Many modern JavaScript engines also do this.
  2. Regardless of implementation (interpreter, VM, machine code), anything you want can run in the background. You can run shell scripts in the background, if you want.
  1. 不,Python 不被解释。标准实现编译为字节码,然后在虚拟机中执行。许多现代 JavaScript 引擎也这样做。
  2. 无论实现(解释器、VM、机器代码)如何,您想要的任何东西都可以在后台运行。如果需要,您可以在后台运行 shell 脚本。

回答by David Z

Technically, Python is compiled to bytecode and then interpreted in a virtual machine. If the Python compiler is able to write out the bytecode into a .pyc file, it will (usually) do so.

从技术上讲,Python 被编译为字节码,然后在虚拟机中进行解释。如果 Python 编译器能够将字节码写入 .pyc 文件,它(通常)会这样做。

On the other hand, there's no explicit compilation step in Python as there is with Java or C. From the point of view of the developer, it looks like Python is just interpreting the .py file directly. Plus, Python offers an interactive prompt where you can type Python statements and have them executed immediately. So the workflow in Python is much more similar to that of an interpreted language than that of a compiled language. To me (and a lot of other developers, I suppose), that distinction of workflow is more important than whether there's an intermediate bytecode step or not.

另一方面,Python 中没有像 Java 或 C 那样显式的编译步骤。从开发人员的角度来看,Python 似乎只是直接解释 .py 文件。此外,Python 提供了一个交互式提示,您可以在其中键入 Python 语句并立即执行它们。因此,Python 中的工作流程与解释语言的工作流程比编译语言的工作流程更相似。对我(我想还有很多其他开发人员)来说,工作流的区别比是否有中间字节码步骤更重要。

回答by John T

Python is an interpreted language but it is the bytecode which is interpreted at run time. There are also many tools out there that can assist you in making your programs run as a windows service / UNIX daemon.

Python 是一种解释型语言,但它是在运行时解释的字节码。还有许多工具可以帮助您使程序作为 Windows 服务/UNIX 守护程序运行。

回答by Phrodo_00

Yes, it's interpreted, its main implementation compiles bytecode first and then runs it though (kind of if you took a java source and the JVM compiled it before running it). Still, you can run your application in background. Actually, you can run pretty much anything in background.

是的,它被解释了,它的主要实现首先编译字节码,然后运行它(如果你使用 java 源代码并且 JVM 在运行它之前编译它)。不过,您可以在后台运行您的应用程序。实际上,您几乎可以在后台运行任何东西。

回答by user90052

Yes, Python is interpreted, but you can also run them as long-running applications.

是的,Python 是解释型的,但您也可以将它们作为长时间运行的应用程序运行。