问题:是Python解释,编译还是两者兼而有之?

据我了解:

一个解释语言是高级语言运行和由解释器执行(程序将高级语言转换成机器代码,然后执行)在旅途中; 它一次只处理一点程序。

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

如果我的定义错误,请纠正我。

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

From my understanding:

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.

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.

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.


回答 0

首先,解释/编译不是语言的属性,而是实现的属性。对于大多数语言,大多数(如果不是全部)实现都归为一类,因此可以省去几个词来说明该语言也可以解释/编译,但这仍然是一个重要的区别,因为这有助于理解,而且语言也很多具有两种可用的实现(主要在功能语言领域,请参见Haskell和ML)。另外,有些C解释器和项目试图将Python的子集编译为C或C ++代码(然后编译为机器代码)。

其次,编译不限于提前编译为本机代码。更一般而言,编译器是将一种编程语言的程序转换为另一种编程语言的程序的程序(可以说,如果应用了重要的转换,甚至可以使编译器使用相同的输入和输出语言)。而且,JIT编译器可以在运行时编译为本地机器代码,这可以使速度与提前编译非常接近甚至更好(取决于基准和所比较实现的质量)。

但是要停止挑剔并回答您要问的问题:实际上(阅读:使用某种流行和成熟的实现),Python被编译了。不会提前编译为机器代码(即受限制和错误的“编译”,但是很常见的定义),“仅”编译为字节码,但它仍然具有至少一些优点。例如,该语句a = b.c()被编译为字节流,当“反汇编”时,该字节流看起来像load 0 (b); load_str 'c'; get_attr; call_function 0; store 1 (a)。这是一种简化,实际上它的可读性较低,而底层则更高级-您可以尝试使用标准库dis模块并查看实际交易的外观。

与参考实现(CPython)一样,该字节码也可以被解释(请注意,直接解释和首先编译为某种中间表示并对其进行解释之间,在理论上和实际性能上都有区别),或者在解释和编译为与PyPy一样,在运行时优化了机器代码。

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.

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.


回答 1

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

Python解释器首先读取人工代码并将其优化为某些中间代码,然后再将其解释为机器代码。这就是为什么您始终需要另一个程序来运行Python脚本的原因,而在C ++中,您可以直接运行代码的已编译可执行文件。例如,c:\Python27\python.exe/usr/bin/python

The CPU can only understand machine code indeed. For interpreted programs, the ultimate goal of an interpreter is to “interpret” the program code into machine code. However, usually a modern interpreted language does not interpret human code directly because it is too inefficient.

The Python interpreter first reads the human code and optimizes it to some intermediate code before interpreting it into machine code. That’s why you always need another program to run a Python script, unlike in C++ where you can run the compiled executable of your code directly. For example, c:\Python27\python.exe or /usr/bin/python.


回答 2

答案取决于正在使用哪种python实现。如果使用的是CPython(Python的标准实现)或Jython(目标是与Java编程语言集成),则首先将其翻译为bytecode,然后根据所使用的python的实现,将此bycode定向到相应的用于解释的虚拟机。用于CPython的PVM(Python虚拟机)和用于Jython的JVM(Java虚拟机)。

但是可以说您正在使用PyPy,这是另一种标准的CPython实现。它将使用即时编译器

The answer depends on what implementation of python is being used. If you are using lets say CPython (The Standard implementation of python) or Jython (Targeted for integration with java programming language)it is first translated into bytecode, and depending on the implementation of python you are using, this bycode is directed to the corresponding virtual machine for interpretation. PVM (Python Virtual Machine) for CPython and JVM (Java Virtual Machine) for Jython.

But lets say you are using PyPy which is another standard CPython implementation. It would use a Just-In-Time Compiler.


回答 3

根据Python的官方网站,它是一种解释语言。

https://www.python.org/doc/essays/blurb/

Python是一种解释型,面向对象的高级编程语言。

由于没有编译步骤…

可以使用Python解释器和广泛的标准库…

相反,当解释器发现错误时,它将引发异常。当程序未捕获异常时,解释器将打印堆栈跟踪。

According to the official Python site, it’s interpreted.

https://www.python.org/doc/essays/blurb/

Python is an interpreted, object-oriented, high-level programming language…

Since there is no compilation step …

The Python interpreter and the extensive standard library are available…

Instead, when the interpreter discovers an error, it raises an exception. When the program doesn’t catch the exception, the interpreter prints a stack trace.


回答 4

是的,它既是编译语言,又是解释语言。 那为什么我们通常将其称为解释语言呢?

看看它是如何被编译和解释的?

首先,我想告诉您,如果您来自Java世界,那么您会更喜欢我的回答。

在Java中,源代码首先通过javac编译器转换为字节代码,然后定向到JVM(负责生成用于执行目的的本机代码)。现在,我想向您展示我们将Java称为编译语言,因为我们可以看到它确实可以编译源代码并通过以下方式提供.class文件(除了字节码外):

javac Hello.java ——->产生Hello.class文件

java Hello ——–>将字节码定向到JVM以执行

python发生了同样的事情,即首先将源代码通过编译器转换为字节码,然后定向到PVM(负责生成用于执行目的的本机代码)。现在,我想向您展示我们通常将Python称为一种解释语言,因为编译是在后台进行的, 并且当我们通过以下方式运行python代码时:

python Hello.py ——->直接执行代码,我们可以看到输出证明该代码在语法上是正确的

@ python Hello.py看起来像直接执行,但实际上它首先生成由解释器解释的字节码,以产生用于执行目的的本机代码。

CPython-负责编译和解释。

如果需要更多详细信息,请查看以下几行

正如我提到的那样,CPython会编译源代码,但是实际的编译是在cython的帮助下发生的,然后解释是在CPython的帮助下发生的

现在让我们谈谈即时编译器在Java和Python中的作用

在JVM中,存在Java解释器,该解释器逐行解释字节码以获取用于执行目的的本机代码,但是当Java字节码由解释器执行时,执行总是较慢。那么解决方案是什么?解决方案是即时编译器,该编译器生成的本机代码可以比解释的快得多地执行。一些JVM供应商使用Java解释器,一些则使用即时编译器。参考:点击这里

在python中绕过解释器以实现快速执行,请使用另一个python实现(PyPy)而不是CPython点击此处查看python的其他实现,包括PyPy

Yes, it is both compiled and interpreted language. Then why we generally call it as interpreted language?

see how it is both- compiled and interpreted?

First of all I want to tell that you will like my answer more if you are from the Java world.

In the Java the source code first gets converted to the byte code through javac compiler then directed to the JVM(responsible for generating the native code for execution purpose). Now I want to show you that we call the Java as compiled language because we can see that it really compiles the source code and gives the .class file(nothing but bytecode) through:

javac Hello.java ——-> produces Hello.class file

java Hello ——–>Directing bytecode to JVM for execution purpose

The same thing happens with python i.e. first the source code gets converted to the bytecode through the compiler then directed to the PVM(responsible for generating the native code for execution purpose). Now I want to show you that we usually call the Python as an interpreted language because the compilation happens behind the scene and when we run the python code through:

python Hello.py ——-> directly excutes the code and we can see the output provied that code is syntactically correct

@ python Hello.py it looks like it directly executes but really it first generates the bytecode that is interpreted by the interpreter to produce the native code for the execution purpose.

CPython– Takes the responsibility of both compilation and interpretation.

Look into the below lines if you need more detail:

As I mentioned that CPython compiles the source code but actual compilation happens with the help of cython then interpretation happens with the help of CPython

Now let’s talk a little bit about the role of Just-In-Time compiler in Java and Python

In JVM the Java Interpreter exists which interprets the bytecode line by line to get the native machine code for execution purpose but when Java bytecode is executed by an interpreter, the execution will always be slower. So what is the solution? the solution is Just-In-Time compiler which produces the native code which can be executed much more quickly than that could be interpreted. Some JVM vendors use Java Interpreter and some use Just-In-Time compiler. Reference: click here

In python to get around the interpreter to achieve the fast execution use another python implementation(PyPy) instead of CPython. click here for other implementation of python including PyPy.


回答 5

如果(您知道Java){

Python代码会像java一样转换为字节码。
每当您尝试访问该字节码时,都会再次执行该字节码。

}其他{

Python代码最初被转换为称为字节码的字节
,它与机器语言非常接近,但与实际的机器代码并不相近,
因此每次我们访问或运行它时,字节码都会再次执行

}

If ( You know Java ) {

Python code is converted into bytecode like java does.
That bytecode is executed again everytime you try to access it.

} else {

Python code is initially traslated into something called bytecode
that is quite close to machine language but not actual machine code
so each time we access or run it that bytecode is executed again

}


回答 6

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

Almost, we can say Python is interpreted language. But we are using some part of one time compilation process in python to convert complete source code into byte-code like java language.


回答 7

对于新手

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

运行脚本不被视为导入,并且不会创建.pyc。

例如,如果您有一个脚本文件abc.py导入了另一个模块xyz.py,则当您运行abc.py时,由于导入了xyz,将创建xyz.pyc,但是从abc开始将不创建abc.pyc文件。 py未导入。

For newbies

Python automatically compiles your script to compiled code, so called byte code, before running it.

Running a script is not considered an import and no .pyc will be created.

For example, if you have a script file abc.py that imports another module xyz.py, when you run abc.py, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc.py isn’t being imported.


回答 8

您编写的python代码将编译为python字节码,从而创建扩展名为.pyc的文件。如果进行编译,那么又一个问题是,为什么不编译语言。

请注意,这不是传统意义上的编译。通常,我们会说编译是采用高级语言并将其转换为机器代码。但这是各种汇编。编译成中间代码而不是机器代码(希望您现在就知道了)。

返回执行过程,然后在适当的虚拟机(在我们的示例中为CPython VM)中执行在编译步骤中创建的,在pyc文件中存在的字节码,然后使用时间戳记(称为幻数)来验证是否。 py文件是否更改,取决于创建的新pyc文件。如果pyc是当前代码,则只需跳过编译步骤。

The python code you write is compiled into python bytecode, which creates file with extension .pyc. If compiles, again question is, why not compiled language.

Note that this isn’t compilation in the traditional sense of the word. Typically, we’d say that compilation is taking a high-level language and converting it to machine code. But it is a compilation of sorts. Compiled in to intermediate code not into machine code (Hope you got it Now).

Back to the execution process, your bytecode, present in pyc file, created in compilation step, is then executed by appropriate virtual machines, in our case, the CPython VM The time-stamp (called as magic number) is used to validate whether .py file is changed or not, depending on that new pyc file is created. If pyc is of current code then it simply skips compilation step.


回答 9

Python(解释器)已编译

证明:如果包含语法错误,甚至不会编译您的代码。

范例1:

print("This should print") 
a = 9/0 

输出:

This should print
Traceback (most recent call last):
  File "p.py", line 2, in <module>
    a = 9/0
ZeroDivisionError: integer division or modulo by zero

代码已成功编译。第一行被执行(print)第二行抛出ZeroDivisionError(运行时错误)。

范例2:

print("This should not print")
/0         

输出:

  File "p.py", line 2
    /0
    ^
SyntaxError: invalid syntax

结论:如果您的代码文件不包含SyntaxError任何内容,则将在编译失败时执行。

Python(the interpreter) is compiled.

Proof: It won’t even compile your code if it contains syntax error.

Example 1:

print("This should print") 
a = 9/0 

Output:

This should print
Traceback (most recent call last):
  File "p.py", line 2, in <module>
    a = 9/0
ZeroDivisionError: integer division or modulo by zero

Code gets compiled successfully. First line gets executed (print) second line throws ZeroDivisionError (run time error) .

Example 2:

print("This should not print")
/0         

Output:

  File "p.py", line 2
    /0
    ^
SyntaxError: invalid syntax

Conclusion: If your code file contains SyntaxError nothing will execute as compilation fails.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。