Python 与 Cpython
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17130975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python vs Cpython
提问by K DawG
What's all this fuss about Python and CPython (Jython,IronPython), I don't get it:
关于 Python 和 CPython (Jython,IronPython) 有什么大惊小怪的,我不明白:
python.orgmentions that CPython is:
python.org提到 CPython 是:
The "traditional" implementation of Python (nicknamed CPython)
Python 的“传统”实现(昵称 CPython)
yet another Stack Overflow questionmentions that:
另一个堆栈溢出问题提到:
CPython is the default byte-code interpreter of Python, which is written in C.
CPython 是 Python 的默认字节码解释器,它是用 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
老实说,我不明白这两种解释的实际含义,但我认为,如果我使用 CPython,这是否意味着当我运行示例 Python 代码时,它会将其编译为 C 语言,然后像 C 语言一样执行它代码
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?
那么 CPython 到底是什么,它与 Python 相比有什么不同,我是否应该使用 CPython 而不是 Python,如果是这样,它的优势是什么?
采纳答案by Martijn Pieters
So what is CPython?
那么什么是 CPython?
CPython is the originalPython 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 languageitself.
CPython 是原始的Python 实现。它是您从 Python.org 下载的实现。人们称它为 CPython 是为了将它与其他后来的 Python 实现区分开来,并将语言引擎的实现与 Python编程语言本身区分开来。
The latter part is where your confusion comes from; you need to keep Python-the-language separate from whatever runsthe Python code.
后一部分是你的困惑来自哪里;您需要将 Python 语言与运行Python 代码的任何内容分开。
CPython happensto 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恰好是用 C 实现的。那只是一个实现细节,真的。CPython 将您的 Python 代码编译为字节码(透明地)并在评估循环中解释该字节码。
CPython is also the first to implement new features; Python-the-language development uses CPython as the base; other implementations follow.
CPython 也是第一个实现新功能的;Python-the-language 开发以 CPython 为基础;其他实现如下。
What about Jython, etc.?
Jython 等呢?
Jython, IronPythonand PyPyare 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 Javabytecode, 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. :-)
Jython、IronPython和PyPy是 Python 编程语言当前的“其他”实现;它们分别用 Java、C# 和 RPython(Python 的一个子集)实现。Jython 将您的 Python 代码编译为Java字节码,以便您的 Python 代码可以在 JVM 上运行。IronPython 允许您在Microsoft CLR上运行 Python 。而 PyPy 是在 Python(的一个子集)中实现的,它可以让您比 CPython 更快地运行 Python 代码,这理所当然地会让您大吃一惊。:-)
Actually compiling to C
实际上编译为C
So CPython does nottranslate your Python code to C by itself. Instead, it runs an interpreter loop. There isa project that doestranslate 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 intothe CPython interpreter.
因此,CPython 不会自行将您的 Python 代码转换为 C。相反,它运行一个解释器循环。还有是一个项目做翻译的Python上下的代码转换为C,而被称为用Cython。用Cython增加了一些扩展Python语言,并让您编译代码,以C扩展,代码插头到CPython的解释。
回答by Abhijit
implementationmeans 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++.
implementation意味着使用什么语言来实现 Python 而不是 Python 代码将如何实现。使用 CPython 的优势是 C 运行时的可用性以及与 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).
所以 CPython 最初是使用C. 原始实现还有其他分支,使 Python 能够利用 Java (JYthon) 或 .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)
根据您使用的实现,库可用性可能会有所不同,例如Ctypes 在 Jython 中不可用,因此任何使用 ctypes 的库在 Jython 中都不起作用。同样,如果要使用 Java 类,则不能直接从 CPython 执行此操作。您要么需要胶水 (JEPP),要么需要使用 Jython(Python 的 Java 实现)
回答by orlenko
Python is a language: a set of rules that can be used to write programs. There are several implementaions of this language.
Python 是一种语言:一组可用于编写程序的规则。这种语言有多种实现方式。
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.
无论你采用什么实现,它们都做几乎相同的事情:获取程序的文本并解释它,执行它的指令。它们都不会将您的代码编译成 C 或任何其他语言。
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.)
CPython 是用 C 编写的原始实现。(“CPython”中的“C”部分是指用于编写 Python 解释器本身的语言。)
Jython is the same language (Python), but implemented using Java.
Jython 是相同的语言 (Python),但使用 Java 实现。
IronPython interpreter was written in C#.
IronPython 解释器是用 C# 编写的。
There's also PyPy - a Python interpreter written in Python. Make your pick :)
还有 PyPy——一个用 Python 编写的 Python 解释器。做出你的选择:)
回答by jamylak
You need to distinguish between a language and an implementation. Python is a language,
您需要区分语言和实现。Python是一种语言,
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 implementationwhich 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.
Python是一种语言。CPython 是 Python 在 C 中的实现。Jython 是在 Java 中的实现,等等。
To sum up: You are already using CPython (if you downloaded from here).
总结:您已经在使用 CPython(如果您从这里下载)。
回答by Oussama L.
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.
您应该知道 CPython 并不真正支持多线程,因为Global Interpreter Lock。它也没有递归优化机制,并且有许多其他实现和库试图填补的其他限制。
You should take a look at this pageon the python wiki.
你应该看看python wiki 上的这个页面。
Look at the code snippets on thispage, it'll give you a good idea of what an interpreter is.
看看这个页面上的代码片段,它会让你很好地了解解释器是什么。
回答by shaktimaan
This articlethoroughly explains the difference between different implementations of Python. Like the article puts it:
这篇文章彻底解释了 Python 不同实现之间的区别。正如文章所说:
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.
首先要意识到“Python”是一个接口。有一个关于 Python 应该做什么以及它应该如何表现的规范(与任何接口一样)。并且有多种实现(与任何接口一样)。
要意识到的第二件事是“解释”和“编译”是实现的属性,而不是接口。
回答by Vijay Kumar
Even I had the same problem understanding how are CPython, JPython, IronPython, PyPy are different from each other.
即使我在理解 CPython、JPython、IronPython、PyPy 之间有何不同时也遇到了同样的问题。
So, I am willing to clear three things before I begin to explain:
所以,我愿意在开始解释之前澄清三件事:
- Python: It is a language, it only states/describes how to convey/express yourself to the interpreter (the program which accepts your python code).
- Implementation: It is all about how the interpreter was written, specifically, in what languageand what it ends up doing.
- 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.
- Python:它是一种语言,它仅说明/描述如何向解释器(接受您的 Python 代码的程序)传达/表达自己。
- 实现:这完全是关于解释器是如何编写的,特别是用什么语言和它最终做什么。
- 字节码:它是由程序处理的代码,通常称为虚拟机,而不是“真正的”计算机机器,硬件处理器。
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).
CPython 是实现,它是用 C 语言编写的。它最终生成 Python 特定的字节码(基于堆栈机器的指令集),然后执行它。将 Python 代码转换为字节码的原因是,如果它看起来像机器指令,那么实现解释器会更容易。但是,没有必要在执行 Python 代码之前生成一些字节码(但 CPython 确实会生成)。
If you want to look at CPython's bytecode then you can. Here's how you can:
如果您想查看 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
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).
现在,让我们看看上面的代码。第 1 到 6 行是函数定义。在第 8 行中,我们导入了“dis”模块,该模块可用于查看由 CPython(解释器)生成的中间 Python 字节码(或者您可以说,Python 字节码的反汇编器)。
NOTE: I got the link to this code from #python IRC channel: https://gist.github.com/nedbat/e89fa710db0edfb9057dc8d18d979f9c
注意:我从#python IRC 频道获得了此代码的链接: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.
然后是 Jython,它是用 Java 编写的,最终生成 Java 字节码。Java 字节码在 Java Runtime Environment 上运行,Java Runtime Environment 是 Java 虚拟机 (JVM) 的实现。如果这令人困惑,那么我怀疑您不知道 Java 是如何工作的。通俗地说,Java(语言,而不是编译器)代码由 Java 编译器获取并输出一个只能使用 JRE 运行的文件(即 Java 字节码)。这样做是为了,一旦 Java 代码被编译,它就可以以 Java 字节码格式移植到其他机器上,只能由 JRE 运行。如果这仍然令人困惑,那么您可能需要查看此网页。
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).
在这里,您可能会问 CPython 的字节码是否像 Jython 一样可移植,我怀疑不是。CPython 实现中生成的字节码特定于该解释器,以便于进一步执行代码(我也怀疑,这种中间字节码的生成,只是为了便于处理在许多其他解释器中完成)。
So, in Jython, when you compile your Python code, you end up with Java byte code, which can be run on a JVM.
因此,在 Jython 中,当您编译 Python 代码时,您最终会得到 Java 字节码,它可以在 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.
同样,IronPython(用 C# 语言编写)将您的 Python 代码编译为公共语言运行时 (CLR),这是一种与 Microsoft 开发的 JVM 类似的技术。
回答by Sushant Chaudhary
The original, and standard, implementation of Pythonis usually called CPythonwhen
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 ActivePythonand
Enthoughtdistributions, 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.
当您想将PythonCPython与其他选项进行对比时,通常会调用Python的原始和标准实现(否则只是简单的“Python”)。这个名字来自于它被编码为可移植的事实ANSI C language code。这是您从http://www.python.org获取的 Python,通过ActivePython和
Enthought发行版获得,并且在大多数 Linux 和 Mac OS X 机器上自动拥有。如果您在您的机器上发现了一个预装的 Python 版本,它可能是
CPython,除非您的公司或组织正在以更专业的方式使用 Python。
Unless you want to script
Javaor.NETapplications with Python or find the benefits ofStacklessorPyPycompelling, you probably want to use the standardCPythonsystem. 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.
除非您想使用 Python编写脚本
Java或.NET应用程序,或者发现其好处Stackless或PyPy引人注目,否则您可能想使用标准CPython系统。因为它是该语言的参考实现,所以与替代系统相比,它往往运行速度最快、最完整,并且更新和健壮。
回答by Premraj
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.
- 解释:解释器将某种语言的程序作为输入,并在某种机器上执行用该语言编写的操作。
- 编译:编译器将某种语言的程序作为输入,并将该程序翻译成另一种语言,该语言可以作为另一个解释器或另一个编译器的输入。
Pythonis an interpreted high-level programming language created by Guido van Rossumin 1991.
Python是由Guido van Rossum于 1991 年创建的解释型高级编程语言。
CPythonis reference version of the Python computing language, which is written in C created by Guido van Rossumtoo.
CPython是 Python 计算语言的参考版本,它也是由Guido van Rossum用 C 语言编写的。

