简体   繁体   English

CPython是字节码解释器吗?

[英]CPython is bytecode interpreter?

I don't really get the concept of "bytecode interpreter" in the context of CPython. 我并没有在CPython的上下文中得到“字节码解释器”的概念。 Can someone shed some light over the whole picture? 有人可以对整个画面有所了解吗?

Does it mean that CPython will compile and execute pyc file (bytecode file?). 这是否意味着CPython将编译并执行pyc文件(字节码文件?)。 Then what compile py file to pyc file? 那么编译py文件到pyc文件是什么? And how is Jython different from CPython (except they are implemented in different languages). Jython与CPython有什么不同(除了它们用不同的语言实现)。

I also read somewhere that Python is C++ interpretation. 我还在某处读过Python是C ++的解释。 Is this correct? 它是否正确? And what does that mean? 这意味着什么?

I'm still very new to Python, so forgive me if I ask the dumb questions... Thank you so much! 我对Python仍然很陌生,所以如果我问一些愚蠢的问题,请原谅我......非常感谢你!

CPython is the implementation of Python in C. It's the first implementation, and still the main one that people mean when they talk about Python. CPython是C语言中Python的实现。它是第一个实现,仍然是人们谈论Python时的主要实现。 It compiles .py files to .pyc files. 它将.py文件编译为.pyc文件。 .pyc files contain bytecodes. .pyc文件包含字节码。 The CPython implementation also interprets those bytecodes. CPython实现还解释了那些字节码。 CPython is not written in C++, it is C. CPython不是用C ++编写的,它是C.

The compilation from .py to .pyc happens transparently as needed. 从.py到.pyc的编译根据需要透明地进行。 When you execute a .py file, it will first be compiled to a .pyc file if needed, then the .pyc file will be interpreted. 执行.py文件时,如果需要,它将首先编译为.pyc文件,然后解释.pyc文件。

Jython is different because (in addition to being implemented in Java instead of C) it compiles .py files into .class files so they can be executed in the JVM. Jython是不同的,因为(除了用Java而不是C实现)它将.py文件编译成.class文件,以便它们可以在JVM中执行。

First: The fact that CPython is a bytecode interpreter should not matter to you as a user of python. 第一:CPython是一个字节码解释器的事实对你来说不应该是python的用户。 Go ahead and write code, and don't worry about how it's turned into action. 继续编写代码,不要担心它是如何变成行动的。

So, to answer your question and satisfy your curiosity, here is what happens: CPython reads python source code, and compiles it into python byte code, which is stored in the .pyc file. 所以,为了回答你的问题并满足你的好奇心,接下来会发生什么:CPython读取python源代码,并将其编译成python字节代码,存储在.pyc文件中。 It then executes that code using a bytecode interpreter. 然后它使用字节码解释器执行该代码。 This design separates the parsing of python from the execution, allowing both parts of the interpreter to be simpler. 这种设计将python的解析与执行分开,允许解释器的两个部分更简单。

Jython is only the front half -- it reads Python source, and outputs Java bytecodes, which are then interpreted by the JVM. Jython只是前半部分 - 它读取Python源代码,并输出Java字节码,然后由JVM解释。 It's the same architecture as CPython, with two noteworthy differences: One: the java bytecode is standardized and documented, while the CPython bytecode is considered internal to python, and can change at any time. 它与CPython具有相同的体系结构,有两个值得注意的差异:一:java字节码是标准化和记录的,而CPython字节码被认为是python内部的,并且可以随时更改。 Two: the JVM is a whole lot more complicated than the CPython interpreter. 二:JVM比CPython解释器复杂得多。 The JVM has one of the most advanced JIT engines in the world, while the CPython interpreter is pretty simple. JVM拥有世界上最先进的JIT引擎之一,而CPython解释器非常简单。

CPython is both the bytecode compiler, and interpreter (virtual machine). CPython既是字节码编译器又是解释器(虚拟机)。

It automatically detects if no existing pre-compiler file (.pyc) exists, compiles the code, and saves it out. 它会自动检测是否存在现有的预编译器文件(.pyc),编译代码并将其保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM