根据我的理解:

解释型语言是一种高级语言,由解释器(将高级语言转换为机器代码并执行的程序)运行并执行;它每次处理一点点程序。

编译语言是一种高级语言,其代码首先由编译器(将高级语言转换为机器代码的程序)转换为机器代码,然后由执行器(另一个运行代码的程序)执行。

如果我的定义错了,请指正。

现在回到Python,我对此有点困惑。你知道Python是一种解释性语言,但它被解释为一些中间代码(如字节码或IL),而不是机器代码。那么,哪个程序执行IM代码呢?请帮助我了解Python脚本是如何处理和运行的。


当前回答

几乎,我们可以说Python是一种解释型语言。但是我们在python中使用了一次性编译过程的一部分,将完整的源代码转换为类似java语言的字节码。

其他回答

在我看来,Python被归入解释器类别,因为它被设计成能够完全处理(从Python代码到在cpu中执行)单个Python语句。也就是说,你写了一个语句,你可以执行它,如果没有错误,然后得到相应的结果。

Having an intermediate code (like bytecode) i believe doesnt make difference to categorize it overall as compiler. Though this component (intermediate code generation) is typically been part of compiler but it can also be used in interpreters. See wiki definition of interpreter https://en.m.wikipedia.org/wiki/Interpreter_(computing). Its a crucial piece to add efficiency in terms of execution speed. With cache its even more powerful so that if you havent changed code in current program scope you skip heavy processing steps like lexical, semantic analysis and even some of code optimization.

CPU实际上只能理解机器代码。对于解释型程序,解释器的最终目标是将程序代码“解释”为机器代码。然而,现代解释语言通常不会直接解释人类代码,因为它效率太低。

Python解释器首先读取人类代码,并在将其解释为机器代码之前将其优化为一些中间代码。这就是为什么你总是需要另一个程序来运行Python脚本,不像在c++中,你可以直接运行编译后的可执行代码。例如,c:\Python27\python.exe或/usr/bin/python.

First off, interpreted/compiled is not a property of the language but a property of the implementation. For most languages, most if not all implementations fall in one category, so one might save a few words saying the language is interpreted/compiled too, but it's still an important distinction, both because it aids understanding and because there are quite a few languages with usable implementations of both kinds (mostly in the realm of functional languages, see Haskell and ML). In addition, there are C interpreters and projects that attempt to compile a subset of Python to C or C++ code (and subsequently to machine code).

Second, compilation is not restricted to ahead-of-time compilation to native machine code. A compiler is, more generally, a program that converts a program in one programming language into a program in another programming language (arguably, you can even have a compiler with the same input and output language if significant transformations are applied). And JIT compilers compile to native machine code at runtime, which can give speed very close to or even better than ahead of time compilation (depending on the benchmark and the quality of the implementations compared).

But to stop nitpicking and answer the question you meant to ask: Practically (read: using a somewhat popular and mature implementation), Python is compiled. Not compiled to machine code ahead of time (i.e. "compiled" by the restricted and wrong, but alas common definition), "only" compiled to bytecode, but it's still compilation with at least some of the benefits. For example, the statement a = b.c() is compiled to a byte stream which, when "disassembled", looks somewhat like load 0 (b); load_str 'c'; get_attr; call_function 0; store 1 (a). This is a simplification, it's actually less readable and a bit more low-level - you can experiment with the standard library dis module and see what the real deal looks like. Interpreting this is faster than interpreting from a higher-level representation.

字节码要么被解释(请注意,在理论和实际性能上,直接解释和首先编译为一些中间表示并解释它之间存在差异),就像参考实现(CPython)一样,要么在运行时被解释并编译为优化的机器代码,就像PyPy一样。

这似乎是语义上的问题。我想大多数人都会推断编译的结果通常是机器代码。考虑到这一点,我对自己说,python是没有编译的。不过我可能错了,因为compile实际上意味着转换到较低级别,所以从源代码转换到字节码也是编译。

这真的取决于所使用的语言的实现!不过,在任何实现中都有一个常见的步骤:您的代码首先被编译(翻译)为中间代码——介于您的代码和机器(二进制)代码之间的东西——称为字节码(存储在.pyc文件中)。注意,这是一个一次性步骤,除非修改代码,否则不会重复。

这个字节码在每次运行程序时都会被执行。怎么做?当我们运行程序时,这个字节码(在.pyc文件中)作为输入传递给虚拟机(VM)1——允许我们的程序被执行的运行时引擎——由虚拟机执行。

根据语言实现,VM将解释字节码(在CPython2实现的情况下)或JIT-compile3字节码(在PyPy4实现的情况下)。

注:

计算机系统的模拟

2字节码解释器;该语言的参考实现,用C和Python编写,使用最广泛

在程序执行期间(在运行时)进行的3个编译

4字节码JIT编译器;CPython的替代实现,用RPython (Restricted Python)编写——通常比CPython运行得更快