根据我的理解:

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

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

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

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


当前回答

你写的python代码被编译成python字节码,它会创建扩展名为.pyc的文件。如果是编译,问题又来了,为什么不是编译语言。

注意,这不是传统意义上的编译。通常,我们会说编译是采用高级语言并将其转换为机器代码。但它是某种汇编。编译到中间代码,而不是机器代码(希望你现在得到它)。

回到执行过程,在编译步骤中创建的pyc文件中的字节码,然后由相应的虚拟机(在我们的例子中是CPython VM)执行 时间戳(称为魔数)用于验证.py文件是否被更改,这取决于创建的新pyc文件。如果pyc是当前代码,那么它简单地跳过编译步骤。

其他回答

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被归入解释器类别,因为它被设计成能够完全处理(从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.

答案取决于使用的是哪种python实现。如果你正在使用CPython (python的标准实现)或Jython(用于与java编程语言集成),它首先被翻译成字节码,并根据你正在使用的python实现,这个字节码被定向到相应的虚拟机进行解释。PVM (Python虚拟机)用于CPython, JVM (Java虚拟机)用于Jython。

但假设你使用的是PyPy,这是另一个标准的CPython实现。它将使用即时编译器。

对于新手

在运行脚本之前,Python会自动将脚本编译为已编译的代码,即所谓的字节代码。

运行脚本不被认为是导入,也不会创建.pyc。

例如,如果你有一个脚本文件abc.py,它导入了另一个模块xyz.py,当你运行abc.py时,xyz.py, xyz.py。Pyc将被创建,因为xyz被导入,但没有abc。Pyc文件将被创建,因为abc.py没有被导入。

如果(你懂Java) { Python代码像java一样被转换成字节码。 每次您尝试访问该字节码时,都会再次执行它。 }其他{ Python代码最初被翻译成一种叫做字节码的东西 接近机器语言,但不是真正的机器代码 因此,每次我们访问或运行它时,字节码都会再次执行 }