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

python.org提到CPython是:

Python的“传统”实现(昵称为CPython)

还有一个Stack Overflow问题提到:

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

老实说,我不明白这两个解释实际上意味着什么,但我想的是,如果我使用CPython,这是否意味着当我运行一个示例python代码时,它会将其编译为C语言,然后像执行C代码一样执行

那么到底什么是CPython,它与python相比有什么不同,我应该使用CPython而不是python,如果是的话,它的优点是什么?


当前回答

Cpython是Python的默认实现,当我们从它的官方网站下载Python时,它就会出现在我们的系统中。

Cpython将扩展名为.py的python源代码文件编译为一个中间字节码,该字节码通常被赋予.pyc扩展名,并由Cpython虚拟机执行。Python的这种实现提供了与Python包和C扩展模块的最大兼容性。

还有许多其他的Python实现,如IronPython, Jython, PyPy, CPython, Stackless Python等等。

其他回答

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

不管你采用什么实现,它们做的事情几乎是一样的:获取程序的文本并解释它,执行它的指令。它们都不会将您的代码编译成C或任何其他语言。

CPython是最初的实现,用C语言编写(“CPython”中的“C”部分指的是用来编写Python解释器本身的语言)。

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

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

还有PyPy——一个用Python编写的Python解释器。你自己选吧。

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

编程语言实现有两种一般方法:

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

Python是一种解释性高级编程语言,由Guido van Rossum于1991年创建。

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

Python实现的其他列表

由于python是开源的,这就是为什么我们可以根据我们的需求定制python。自定义之后,我们可以根据需要命名该版本。这就是为什么python有多种口味。每种口味都是python的定制版本,以满足特殊要求。这类似于UNIX有多种版本,比如Ubuntu、Linux、RedHat Linux等等。以下是python的一些风味:

CPython

Default implementation of python programming language which we download from python.org, provided by python software foundation. It is written in C and python. It does not allow us to write any C code, only python code are allowed. CPython can be called as both an interpreter and a compiler as here our python code first gets compiled to python bytecode then the bytecode gets interpreted into platform-specific operations by PVM or Python Virtual Machine. Keep in mind interpreters have language syntaxes predefined that's why it does not need to translate into low level machine code. Here Interpreter just executes bytecode on the fly during runtime and results in platform-specific operations.

Old versions of JavaScript, Ruby, Php were fully interpreted languages as their interpreters would directly translate each line source code into platform-specific operations, no bytecode was involved. Bytecode is there in Java, Python, C++ (.net), C# to decouple the language from execution environment, i.e. for portability, write once, run anywhere. Since 2008, Google Chrome's V8 JavaScript engine has come up with Just-In-Time Compiler for JavaScript. It executes JavaScript code line-by-line like an interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using baseline or optimizing compiler.

Cython

Cython是一种编程语言,它是python和C的超集。它是用C和python编写的。它的设计目的是通过python语法和可选的c语法提供类似c的性能。Cython是一种编译语言,因为它生成C代码并由C编译器编译。我们可以在Cython中编写与默认python或CPython中类似的代码,区别是:

Cython允许我们编写可选的额外C代码, 在Cython中,我们的python代码在内部被翻译成C代码,以便C编译器可以编译它。虽然Cython的执行速度快得多,但与原始C语言的执行速度相比还是差得多。这是因为Cython必须调用CPython解释器和CPython标准库来理解编写的CPython代码

吉通/吉通

Java实现的python编程语言。它是用Java和python编写的。在这里,我们的python代码首先被编译为Java字节码,然后由JVM或Java虚拟机将字节码解释为特定于平台的操作。这类似于Java代码的执行方式:Java代码首先被编译为中间字节码,然后由JVM将该字节码解释为特定于平台的操作

PyPy

RPython implementation of python programming language. It is written in a restricted subset of python called Restricted Python (RPython). PyPy runs faster than CPython because to interpret bytecode, PyPy has a Just-in-time Compiler (Interpreter + Compiler) while CPython has an Interpreter. So JIT Compiler in PyPy can execute Python bytecode line-by-line like an Interpreter to reduce start up time, but if encounters with a hot section with repeatedly executed line of code then optimizes that code using Baseline or Optimizing Compiler.

JIT Compiler in a Nutshell: Compiler in Python translates our high level source code into bytecode and to execute bytecode, some implementations have normal Interpreter, some have Just-in-time Compiler. To execute a loop which runs say, million times i.e. a very hot code, initially Interpreter will run it for some time and Monitor of JIT Compiler will watch that code. Then when it gets repeated some times i.e. the code becomes warm* then JIT compiler will send that code to Baseline Compiler which will make some assumptions on variable types etc. based on data gathered by Monitor while watching the code. From next iterations if assumptions turn out to be valid, then no need to retranslate bytecode into machine code, i.e. steps can be skipped for faster execution. If the code repeats a lot of times i.e. code becomes very hot then JIT compiler will send that code to Optimizing Compiler which will make more assumptions and will skip more steps for very fast execution.

JIT Compiler Drawbacks: initial slower execution when the code gets analysed, and if assumptions turn out to be false then optimized compiled code gets thrown out i.e. Deoptimization or Bailing out happens which can make code execution slower, although JIT compilers has limit for optimization/deoptimization cycle. After certain number of deoptimization happens, JIT Compiler just does not try to optimize anymore. Whereas normal Interpreter, in each iteration, repeatedly translates bytecode into machine code thereby taking more time to complete a loop which runs say, million times

IronPython

c#实现的python,针对。net框架

Ruby Python

支持Ruby平台

蟒蛇Python

python和R编程语言用于科学计算,如数据科学、机器学习、人工智能、深度学习、处理大量数据等。大量的库,如scikit-learn, tensorflow, pytorch, numba, pandas, jupyter, numpy, matplotlib等都可以使用这个包

Stackless

Python for并发

为了测试每个实现的速度,我们编写了一个程序,使用N值50,000调用integrate_f 500次,并记录几次运行的执行时间。下表显示了基准测试结果:

Implementation Execution Time (seconds) Speed Up
CPython 9.25
CPython + Cython 0.21 44x
PyPy 0.57 16x

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.

除非你想用Python编写Java或。net应用程序的脚本,或者找到其中的好处 如果你不想使用Stackless或PyPy,你可能想使用标准的CPython系统。 因为它是该语言的参考实现,所以它倾向于运行 最快、最完整、比替代方案更新更及时、更健壮 系统。

你应该知道CPython并不真正支持多线程(它支持,但不是最优的),因为全局解释器锁。它也没有递归的优化机制,并且有许多其他实现和库试图填补的限制。

你应该看看python wiki上的这个页面。

看看本页上的代码片段,它会让你很好地了解解释器是什么。