Python vs Cpython

本文翻译自:Python vs Cpython

What's all this fuss about Python and CPython (Jython,IronPython) , I don't get it: 什么是关于Python和CPython (Jython,IronPython)的所有这些大惊小怪,我不明白:

python.org mentions that CPython is: python.org提到CPython是:

The "traditional" implementation of Python (nicknamed CPython) Python的“传统”实现(绰号CPython)

yet another Stack Overflow question mentions that: 另一个Stack Overflow问题提到:

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,如果有的话,它的优点是什么?


#1楼

参考:https://stackoom.com/question/19sY3/Python-vs-Cpython


#2楼

So what is CPython? 那么CPython是什么?

CPython is the original Python implementation. CPython是最初的 Python实现。 It is the implementation you download from Python.org. 这是您从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. 人们将其称为CPython,以区别于其他后来的Python实现,并将语言引擎的实现与Python 编程语言本身区分开来。

The latter part is where your confusion comes from; 后半部分是你的困惑所在; you need to keep Python-the-language separate from whatever runs the Python code. 你需要将Python-the-language与运行 Python代码的任何东西分开。

CPython happens to be implemented in C. That is just an implementation detail, really. CPython 恰好用C实现。这只是一个实现细节,真的。 CPython compiles your Python code into bytecode (transparently) and interprets that bytecode in a evaluation loop. CPython将您的Python代码编译为字节码(透明地)并在评估循环中解释该字节码。

CPython is also the first to implement new features; CPython也是第一个实现新功能的人; Python-the-language development uses CPython as the base; Python-the-language开发使用CPython作为基础; other implementations follow. 其他实现如下。

What about Jython, etc.? Jython等怎么样?

Jython , IronPython and PyPy are the current "other" implementations of the Python programming language; JythonIronPythonPyPy是Python编程语言的当前“其他”实现; these are implemented in Java, C# and RPython (a subset of Python), respectively. 它们分别在Java,C#和RPython(Python的一个子集)中实现。 Jython compiles your Python code to Java bytecode, so your Python code can run on the JVM. Jython将您的Python代码编译为Java字节码,因此您的Python代码可以在JVM上运行。 IronPython lets you run Python on the Microsoft CLR . IronPython允许您在Microsoft CLR上运行Python。 And PyPy, being implemented in (a subset of) Python, lets you run Python code faster than CPython, which rightly should blow your mind. PyPy是在Python的(子集)中实现的,它允许您比CPython更快地运行Python代码,这应该让您大吃一惊。 :-) :-)

Actually compiling to C 实际编译为C

So CPython does not translate your Python code to C by itself. 因此CPython的本身翻译你的Python代码转换为C。 Instead, it runs an interpreter loop. 相反,它运行一个解释器循环。 There is a project that does translate Python-ish code to C, and that is called Cython . 目前的Python翻译-ISH代码转换为C的一个项目,那就是所谓的用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. 用Cython增加了一些扩展Python语言,并让您编译代码,以C扩展,可以插入 CPython的解释器代码。


#3楼

implementation means what language was used to implement Python and not how python Code would be implemented. implementation意味着使用什么语言来实现Python,而不是如何实现python Code。 The advantage of using CPython is the availability of C Run-time as well as easy integration with C/C++. 使用CPython的优点是C Run-time的可用性以及与C / C ++的轻松集成。

So CPython was originally implemented using C . 所以CPython最初是用C实现的。 There were other forks to the original implementation which enabled Python to lever-edge Java (JYthon) or .NET Runtime (IronPython). 原始实现还有其他分支,使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. 根据您使用的实现,库可用性可能会有所不同,例如Ctypes在Jython中不可用 ,因此任何使用ctypes的库都无法在Jython中使用。 Similarly, if you want to use a Java Class, you cannot directly do so from CPython. 同样,如果要使用Java类,则无法直接从CPython执行此操作。 You either need a glue (JEPP) or need to use Jython (The Java Implementation of Python) 你需要一个胶水(JEPP)或需要使用Jython(Python的Java实现)


#4楼

Python is a language: a set of rules that can be used to write programs. Python是一种语言:一组可用于编写程序的规则。 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. 他们都没有将您的代码编译成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. 还有PyPy - 一个用Python编写的Python解释器。 Make your pick :) 你的选择:)


#5楼

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 implementation which in most cases, is the actual interpreter or compiler. 另外,我们有一个编程语言实现 ,在大多数情况下,它是实际的解释器或编译器。

Python is a language. Python是一种语言。 CPython is the implementation of Python in C. Jython is the implementation in Java, and so on. CPython是C中Python的实现.Jython是Java中的实现,依此类推。

To sum up: You are already using CPython (if you downloaded from here ). 总结一下:您已经在使用CPython(如果您从此处下载)。


#6楼

You should know that CPython doesn't really support multithreading because of the Global Interpreter Lock . 您应该知道,由于Global Interpreter Lock ,CPython并不真正支持多线程。 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. 你应该看看python wiki上的这个页面

Look at the code snippets on this page, it'll give you a good idea of what an interpreter is. 看看这个页面上的代码片段,它会让你很好地了解解释器是什么。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值