根据我的理解:

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

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

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

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


当前回答

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

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

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

其他回答

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

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运行我们的脚本时,在我们的代码实际开始处理之前,Python会执行几个步骤:

它被编译为字节码。 然后路由到虚拟机。

当我们执行一些源代码时,Python会将其编译成字节代码。编译是一个转换步骤,字节代码是源代码的底层平台独立表示。

注意,Python字节码不是二进制机器码(例如,英特尔芯片的指令)。

实际上,Python通过将源代码的每个语句分解为单独的步骤,将它们转换为字节码指令。执行字节码转换以加快执行速度。 字节代码的运行速度比原始源代码语句快得多。它有。Pyc扩展,它将被写入,如果它可以写入到我们的机器。

因此,下次我们运行相同的程序时,Python将加载.pyc文件并跳过编译步骤,除非它已被更改。Python自动检查源文件和字节码文件的时间戳,以知道何时必须重新编译。如果我们重新保存源代码,字节码将在下次程序运行时再次自动创建。

如果Python不能将字节代码文件写入我们的机器,我们的程序仍然可以工作。字节码在内存中生成,并在程序退出时简单地丢弃。但是由于.pyc文件加快了启动时间,我们可能希望确保它是为较大的程序编写的。

Let's summarize what happens behind the scenes. When Python executes a program, Python reads the .py into memory, and parses it in order to get a bytecode, then goes on to execute. For each module that is imported by the program, Python first checks to see whether there is a precompiled bytecode version, in a .pyo or .pyc, that has a timestamp which corresponds to its .py file. Python uses the bytecode version if any. Otherwise, it parses the module's .py file, saves it into a .pyc file, and uses the bytecode it just created.

字节代码文件也是传递Python代码的一种方式。Python仍然会运行一个程序,如果它能找到的都是。Pyc文件,即使原始的.py源文件不在那里。

Python虚拟机(PVM)

Once our program has been compiled into byte code, it is shipped off for execution to Python Virtual Machine (PVM). The PVM is not a separate program. It need not be installed by itself. Actually, the PVM is just a big loop that iterates through our byte code instruction, one by one, to carry out their operations. The PVM is the runtime engine of Python. It's always present as part of the Python system. It's the component that truly runs our scripts. Technically it's just the last step of what is called the Python interpreter.

As sone one already said, "interpreted/compiled is not a property of the language but a property of the implementation." Python can be used in interpretation mode as well as compilation mode. When you run python code directly from terminal or cmd then the python interpreter starts. Now if you write any command then this command will be directly interpreted. If you use a file containing Python code and running it in IDE or using a command prompt it will be compiled first the whole code will be converted to byte code and then it will run. So it depends on how we use it.

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

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