问题描述
根据我的理解:
解释性语言是一种高级语言,由解释器(一种将高级语言转换为机器代码然后执行的程序)在旅途中运行和执行;它一次处理一些程序.
An interpreted language is a high-level language run and executed by an interpreter (a program which converts the high-level language to machine code and then executing) on the go; it processes the program a little at a time.
编译语言是一种高级语言,其代码首先由编译器(将高级语言转换为机器代码的程序)转换为机器码,然后由编译器执行executor(另一个运行代码的程序).
A compiled language is a high-level language whose code is first converted to machine-code by a compiler (a program which converts the high-level language to machine code) and then executed by an executor (another program for running the code).
如果我的定义有误,请纠正我.
Correct me if my definitions are wrong.
现在回到 Python,我对此有点困惑.你到处都知道 Python 是一种解释型语言,但它被解释为一些中间代码(如字节码或 IL),不解释为机器代码.那么哪个程序执行IM代码呢?请帮助我了解如何处理和运行 Python 脚本.
Now coming back to Python, I am bit confused about this. Everywhere you learn that Python is an interpreted language, but it's interpreted to some intermediate code (like byte-code or IL) and not to the machine code. So which program then executes the IM code? Please help me understand how a Python script is handled and run.
推荐答案
首先,解释/编译不是语言的属性,而是实现的属性.对于大多数语言,大多数(如果不是全部)实现都属于一个类别,因此人们可能会保留几句话说该语言也被解释/编译,但这仍然是一个重要的区别,既因为它有助于理解,又因为有很多语言具有两种可用的实现(主要在函数式语言领域,参见 Haskell 和 ML).此外,还有一些 C 解释器和项目试图将 Python 的一个子集编译为 C 或 C++ 代码(随后编译为机器代码).
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).
其次,编译不限于提前编译为本机机器代码.更一般地说,编译器是将一种编程语言的程序转换为另一种编程语言的程序的程序(可以说,如果应用了重要的转换,您甚至可以拥有具有相同输入和输出语言的编译器).并且 JIT 编译器在运行时编译为本机机器代码,这可以使速度非常接近甚至优于提前编译(取决于基准和比较实现的质量).
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).
但是为了停止挑剔并回答您想问的问题:实际上(阅读:使用有点流行和成熟的实现),Python 是编译的.未提前编译为机器码(即编译"受限制和错误,但很常见的定义),仅"编译为 字节码,但它仍然具有至少一些好处的编译.例如,语句 a = b.c()
被编译成一个字节流,当反汇编"时,它看起来有点像 load 0 (b);load_str 'c';get_attr;call_function 0;存储1(a)
.这是一种简化,它实际上可读性较差,而且级别较低 - 您可以尝试使用标准库 dis
模块,看看真正的交易是什么样的.解释这个比从更高级别的表示解释更快.
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 一样.
That bytecode is either interpreted (note that there's a difference, both in theory and in practical performance, between interpreting directly and first compiling to some intermediate representation and interpret that), as with the reference implementation (CPython), or both interpreted and compiled to optimized machine code at runtime, as with PyPy.
这篇关于Python 是解释的还是编译的,或者两者兼而有之?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!