问题:Python与Cpython

关于Python和CPython (Jython,IronPython)的所有这些大惊小怪,我不明白:

python.org提到CPython是:

Python的“传统”实现(绰号为CPython)

另一个堆栈溢出问题提到:

CPython是Python的默认字节码解释器,它是用C编写的。

老实说,我并没有得到这两种解释的实际含义,但是我认为的是,如果我使用CPython,那意味着当我运行示例python代码时,它将其编译为C语言,然后像执行C语言一样执行它码

那么CPython到底是什么?与python相比,它有什么区别?我应该在Python上使用CPython吗?如果有,它的优点是什么?

What’s all this fuss about Python and CPython (Jython,IronPython), I don’t get it:

python.org mentions that CPython is:

The “traditional” implementation of Python (nicknamed CPython)

yet another Stack Overflow question mentions that:

CPython is the default byte-code interpreter of Python, which is written in C.

Honestly I don’t get what both of those explanations practically mean but what I thought was that, if I use CPython does that mean when I run a sample python code, it compiles it to C language and then executes it as if it were C code

So what exactly is CPython and how does it differ when compared with python and should I probably use CPython over Python and if so what are its advantages?


回答 0

那么CPython是什么?

CPython是原始的 Python实现。它是您从Python.org下载的实现。人们称它为CPython是为了将其与其他后来的Python实现区分开来,并将语言引擎的实现与Python 编程语言本身区分开来。

后面的部分是您困惑的来源。您需要将Python语言与运行 Python代码的代码分开。

CPython 恰好用C实现。实际上,这只是实现细节。CPython将您的Python代码(透明地)编译为字节码,并在评估循环中解释该字节码。

CPython也是第一个实现新功能的人。Python语言开发使用CPython作为基础。其他实现如下。

Jython等如何?

JythonIronPythonPyPy是Python编程语言的当前“其他”实现。它们分别用Java,C#和RPython(Python的子集)实现。Jython将您的Python代码编译为Java字节码,因此您的Python代码可以在JVM上运行。IronPython使您可以在Microsoft CLR上运行Python 。而且,PyPy(在Python(的一部分)中实现)使您可以比CPython更快地运行Python代码,这应该引起您的注意。:-)

实际编译为C

因此,CPython本身不会将您的Python代码转换为C。而是运行解释器循环。还有一个项目翻译的Python上下的代码转换为C,而被称为用Cython。用Cython增加了一些扩展Python语言,并让您编译代码,以C扩展,代码插头 CPython的解释。

So what is CPython?

CPython is the original Python implementation. It is the implementation you download from Python.org. People call it CPython to distinguish it from other, later, Python implementations, and to distinguish the implementation of the language engine from the Python programming language itself.

The latter part is where your confusion comes from; you need to keep Python-the-language separate from whatever runs the Python code.

CPython happens to be implemented in C. That is just an implementation detail, really. CPython compiles your Python code into bytecode (transparently) and interprets that bytecode in a evaluation loop.

CPython is also the first to implement new features; Python-the-language development uses CPython as the base; other implementations follow.

What about Jython, etc.?

Jython, IronPython and PyPy are the current “other” implementations of the Python programming language; these are implemented in Java, C# and RPython (a subset of Python), respectively. Jython compiles your Python code to Java bytecode, so your Python code can run on the JVM. IronPython lets you run Python on the Microsoft CLR. And PyPy, being implemented in (a subset of) Python, lets you run Python code faster than CPython, which rightly should blow your mind. :-)

Actually compiling to C

So CPython does not translate your Python code to C by itself. Instead, it runs an interpreter loop. There is a project that does translate Python-ish code to C, and that is called Cython. Cython adds a few extensions to the Python language, and lets you compile your code to C extensions, code that plugs into the CPython interpreter.


回答 1

您需要区分语言和实现。Python是一种语言

根据Wikipedia所说,“编程语言是用于编写程序的一种表示法,它是一种计算或算法的规范”。这意味着它只是编写代码的规则和语法。另外,我们有一个编程语言实现,在大多数情况下是实际的解释器或编译器。

Python是一种语言。CPython是C语言中Python的实现。Jython是Java语言中的实现,依此类推。

总结:您已经在使用CPython(如果从此处下载)。

You need to distinguish between a language and an implementation. Python is a language,

According to Wikipedia, “A programming language is a notation for writing programs, which are specifications of a computation or algorithm”. This means that it’s simply the rules and syntax for writing code. Separately we have a programming language implementation which in most cases, is the actual interpreter or compiler.

Python is a language. CPython is the implementation of Python in C. Jython is the implementation in Java, and so on.

To sum up: You are already using CPython (if you downloaded from here).


回答 2

甚至我在理解CPython,JPython,IronPython,PyPy之间的区别时也遇到了相同的问题。

因此,在开始解释之前,我愿意清除三件事:

  1. Python:这是一门语言,它仅说明/描述如何向解释器(接受您的python代码的程序)传达/表达自己。
  2. 实现:一切都与解释器的编写方式有关,特别是关于哪种语言以及最终使用的语言
  3. 字节码:它是由程序(通常称为虚拟机)而不是“真实”计算机(即硬件处理器)处理的代码。

CPython是用C语言编写的实现。它最终生成特定于Python的字节码(基于堆栈计算机的指令集),然后执行它。将Python代码转换为字节码的原因是,如果看起来像机器指令,则更容易实现解释器。但是,没有必要在执行Python代码之前产生一些字节码(但CPython确实会产生)。

如果您想查看CPython的字节码,则可以。方法如下:

>>> def f(x, y):                # line 1
...    print("Hello")           # line 2
...    if x:                    # line 3
...       y += x                # line 4
...    print(x, y)              # line 5
...    return x+y               # line 6
...                             # line 7
>>> import dis                  # line 8
>>> dis.dis(f)                  # line 9
  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello')
              4 CALL_FUNCTION            1
              6 POP_TOP

  3           8 LOAD_FAST                0 (x)
             10 POP_JUMP_IF_FALSE       20

  4          12 LOAD_FAST                1 (y)
             14 LOAD_FAST                0 (x)
             16 INPLACE_ADD
             18 STORE_FAST               1 (y)

  5     >>   20 LOAD_GLOBAL              0 (print)
             22 LOAD_FAST                0 (x)
             24 LOAD_FAST                1 (y)
             26 CALL_FUNCTION            2
             28 POP_TOP

  6          30 LOAD_FAST                0 (x)
             32 LOAD_FAST                1 (y)
             34 BINARY_ADD
36 RETURN_VALUE

现在,让我们看一下上面的代码。第1至6行是功能定义。在第8行中,我们导入了“ dis”模块,该模块可用于查看由CPython(解释器)生成的中间Python字节码(或者可以说是Python字节码的反汇编程序)。

注意:我从#python IRC频道获得了此代码的链接:https ://gist.github.com/nedbat/e89fa710db0edfb9057dc8d18d979f9c

然后是Jython,它是用Java编写的,最终生成Java字节码。Java字节代码在Java运行时环境上运行,该环境是Java虚拟机(JVM)的实现。如果这令人困惑,那么我怀疑您不知道Java如何工作。用外行术语来说,Java编译器采用Java(语言,而不是编译器)代码,并输出只能使用JRE运行的文件(Java字节码)。这样做的目的是,一旦编译了Java代码,就可以将其以Java字节代码格式移植到其他计算机上,该格式只能由JRE运行。如果仍然令人困惑,那么您可能想看看该网页

在这里,您可能会问CPython的字节码是否像Jython一样可移植,我怀疑不是。CPython实现中生成字节码特定于该解释器,以便于进一步执行代码(我还怀疑,这种中间字节码的产生只是为了在许多其他解释器中简化处理)。

因此,在Jython中,当您编译Python代码时,最终会得到Java字节代码,该代码可以在JVM上运行。

同样,IronPython(用C#语言编写)将您的Python代码编译为公共语言运行时(CLR),与Microsoft开发的JVM相比,这项技术是类似的。

Even I had the same problem understanding how are CPython, JPython, IronPython, PyPy are different from each other.

So, I am willing to clear three things before I begin to explain:

  1. Python: It is a language, it only states/describes how to convey/express yourself to the interpreter (the program which accepts your python code).
  2. Implementation: It is all about how the interpreter was written, specifically, in what language and what it ends up doing.
  3. Bytecode: It is the code that is processed by a program, usually referred to as a virtual machine, rather than by the “real” computer machine, the hardware processor.

CPython is the implementation, which was written in C language. It ends up producing bytecode (stack-machine based instruction set) which is Python specific and then executes it. The reason to convert Python code to a bytecode is because it’s easier to implement an interpreter if it looks like machine instructions. But, it isn’t necessary to produce some bytecode prior to execution of the Python code (but CPython does produce).

If you want to look at CPython’s bytecode then you can. Here’s how you can:

>>> def f(x, y):                # line 1
...    print("Hello")           # line 2
...    if x:                    # line 3
...       y += x                # line 4
...    print(x, y)              # line 5
...    return x+y               # line 6
...                             # line 7
>>> import dis                  # line 8
>>> dis.dis(f)                  # line 9
  2           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('Hello')
              4 CALL_FUNCTION            1
              6 POP_TOP

  3           8 LOAD_FAST                0 (x)
             10 POP_JUMP_IF_FALSE       20

  4          12 LOAD_FAST                1 (y)
             14 LOAD_FAST                0 (x)
             16 INPLACE_ADD
             18 STORE_FAST               1 (y)

  5     >>   20 LOAD_GLOBAL              0 (print)
             22 LOAD_FAST                0 (x)
             24 LOAD_FAST                1 (y)
             26 CALL_FUNCTION            2
             28 POP_TOP

  6          30 LOAD_FAST                0 (x)
             32 LOAD_FAST                1 (y)
             34 BINARY_ADD
36 RETURN_VALUE

Now, let’s have a look at the above code. Lines 1 to 6 are a function definition. In line 8, we import the ‘dis’ module which can be used to view the intermediate Python bytecode (or you can say, disassembler for Python bytecode) that is generated by CPython (interpreter).

NOTE: I got the link to this code from #python IRC channel: https://gist.github.com/nedbat/e89fa710db0edfb9057dc8d18d979f9c

And then, there is Jython, which is written in Java and ends up producing Java byte code. The Java byte code runs on Java Runtime Environment, which is an implementation of Java Virtual Machine (JVM). If this is confusing then I suspect that you have no clue how Java works. In layman terms, Java (the language, not the compiler) code is taken by the Java compiler and outputs a file (which is Java byte code) that can be run only using a JRE. This is done so that, once the Java code is compiled then it can be ported to other machines in Java byte code format, which can be only run by JRE. If this is still confusing then you may want to have a look at this web page.

Here, you may ask if the CPython’s bytecode is portable like Jython, I suspect not. The bytecode produced in CPython implementation was specific to that interpreter for making it easy for further execution of code (I also suspect that, such intermediate bytecode production, just for the ease the of processing is done in many other interpreters).

So, in Jython, when you compile your Python code, you end up with Java byte code, which can be run on a JVM.

Similarly, IronPython (written in C# language) compiles down your Python code to Common Language Runtime (CLR), which is a similar technology as compared to JVM, developed by Microsoft.


回答 3

文章详细地介绍了Python中的不同实现之间的区别。如文章所述:

首先要意识到的是“ Python”是一个接口。有关于Python应该做什么以及应该如何表现的规范(与任何接口一样)。并且有多种实现方式(与任何接口一样)。

要了解的第二件事是,“解释”和“编译”是实现的属性,而不是接口。

This article thoroughly explains the difference between different implementations of Python. Like the article puts it:

The first thing to realize is that ‘Python’ is an interface. There’s a specification of what Python should do and how it should behave (as with any interface). And there are multiple implementations (as with any interface).

The second thing to realize is that ‘interpreted’ and ‘compiled’ are properties of an implementation, not an interface.


回答 4

Python是一种语言:一组可用于编写程序的规则。该语言有几种实现方式。

不管您采用哪种实现,它们都差不多做同样的事情:获取程序的文本并解释它,执行其指令。它们都没有将您的代码编译为C或任何其他语言。

CPython是用C编写的原始实现。(“ CPython”中的“ C”部分是指用于编写Python解释器本身的语言。)

Jython是相同的语言(Python),但是使用Java实现。

IronPython解释器是用C#编写的。

还有PyPy-用Python编写的Python解释器。选你一个:)

Python is a language: a set of rules that can be used to write programs. There are several implementaions of this language.

No matter what implementation you take, they do pretty much the same thing: take the text of your program and interpret it, executing its instructions. None of them compile your code into C or any other language.

CPython is the original implementation, written in C. (The “C” part in “CPython” refers to the language that was used to write Python interpreter itself.)

Jython is the same language (Python), but implemented using Java.

IronPython interpreter was written in C#.

There’s also PyPy – a Python interpreter written in Python. Make your pick :)


回答 5

implementation表示使用什么语言来实现Python,而不是如何实现python代码。使用CPython的优点是C运行时的可用性以及与C / C ++的轻松集成。

因此CPython最初是使用来实现的C。原始实现还有其他方面,使Python能够利用Java(JYthon)或.NET Runtime(IronPython)进行开发。

根据您使用的实现,库可用性可能会有所不同,例如Jython中不提供Ctypes,因此任何使用ctypes的库在Jython中均不起作用。同样,如果要使用Java类,则不能直接从CPython中使用。您需要胶水(JEPP)或需要使用Jython(Python的Java实现)

implementation means what language was used to implement Python and not how python Code would be implemented. The advantage of using CPython is the availability of C Run-time as well as easy integration with C/C++.

So CPython was originally implemented using C. There were other forks to the original implementation which enabled Python to lever-edge Java (JYthon) or .NET Runtime (IronPython).

Based on which Implementation you use, library availability might vary, for example Ctypes is not available in Jython, so any library which uses ctypes would not work in Jython. Similarly, if you want to use a Java Class, you cannot directly do so from CPython. You either need a glue (JEPP) or need to use Jython (The Java Implementation of Python)


回答 6

您应该知道,由于全局解释器锁,CPython并不真正支持多线程。它还没有用于递归的优化机制,并具有其他实现和库试图填补的许多其他限制。

您应该在python Wiki上查看此页面

查看页面上的代码片段,它将使您对解释器的含义有所了解。

You should know that CPython doesn’t really support multithreading because of the Global Interpreter Lock. It also has no Optimisation mechanisms for recursion, and has many other limitations that other implementations and libraries try to fill.

You should take a look at this page on the python wiki.

Look at the code snippets on this page, it’ll give you a good idea of what an interpreter is.


回答 7

当您想将其与其他选项进行对比时,通常会调用Python的原始和标准实现CPython否则,仅使用普通的“ Python”)。这个名字来自于事实,它被编码为可移植的ANSI C language code。这是您从http://www.python.org获取的Python,与ActivePythonEnthought发行版一起获得,并且已在大多数Linux和Mac OS X计算机上自动安装。如果您在计算机上找到了预装的Python版本,则可能是 CPython,除非您的公司或组织以更专业的方式使用Python。

除非您想使用Python 编写脚本Java.NET应用程序,或者想从中受益StacklessPyPy引人注目,否则您可能要使用标准CPython系统。因为它是该语言的参考实现,所以与替代系统相比,它往往运行最快,最完整,并且更新和更健壮。

The original, and standard, implementation of Python is usually called CPython when you want to contrast it with the other options (and just plain “Python” otherwise). This name comes from the fact that it is coded in portable ANSI C language code. This is the Python that you fetch from http://www.python.org, get with the ActivePython and Enthought distributions, and have automatically on most Linux and Mac OS X machines. If you’ve found a preinstalled version of Python on your machine, it’s probably CPython, unless your company or organization is using Python in more specialized ways.

Unless you want to script Java or .NET applications with Python or find the benefits of Stackless or PyPy compelling, you probably want to use the standard CPython system. Because it is the reference implementation of the language, it tends to run the fastest, be the most complete, and be more up-to-date and robust than the alternative systems.


回答 8

编程语言实现是用于执行计算机程序的系统。

编程语言实现有两种通用方法:

  • 解释:解释器将某种语言的程序作为输入,并在某种机器上执行以该语言编写的动作。
  • 编译:编译器将某种语言的程序作为输入,并将该程序翻译为某种其他语言,该语言可以用作另一解释器或另一编译器的输入。

PythonGuido van Rossum在1991年创建的一种解释型高级编程语言。

CPython是Python计算语言的参考版本,也是由Guido van Rossum创建的用C编写的。

其他Python实施清单

资源

A programming language implementation is a system for executing computer programs.

There are two general approaches to programming language implementation:

  • Interpretation: An interpreter takes as input a program in some language, and performs the actions written in that language on some machine.
  • Compilation: A compiler takes as input a program in some language, and translates that program into some other language, which may serve as input to another interpreter or another compiler.

Python is an interpreted high-level programming language created by Guido van Rossum in 1991.

CPython is reference version of the Python computing language, which is written in C created by Guido van Rossum too.

Other list of Python Implementations

Source


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