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

python.org提到CPython是:

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

还有一个Stack Overflow问题提到:

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

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

那么到底什么是CPython,它与python相比有什么不同,我应该使用CPython而不是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

其他回答

本文详细解释了Python的不同实现之间的区别。就像这篇文章说的:

首先要意识到“Python”是一个接口。有一个 Python应该做什么以及它应该如何表现(如 任何接口)。并且有多种实现(如 任何接口)。 要意识到的第二件事是“解释”和“编译” 实现的属性,而不是接口的属性。

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实现)

由于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

那么什么是CPython呢?

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

后一部分是你困惑的来源;你需要将Python语言与运行Python代码的任何东西分开。

CPython恰好是用c实现的,这只是一个实现细节。CPython将Python代码编译为字节码(透明地),并在计算循环中解释该字节码。

CPython也是第一个实现新特性的;python语言开发以CPython为基础;接下来是其他实现。

Jython等等呢?

Jython, IronPython和PyPy是目前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解释器中的代码。

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

根据维基百科,“编程语言是编写程序的符号,是计算或算法的规范”。这意味着它只是编写代码的规则和语法。另外,我们有一个编程语言实现,在大多数情况下,是实际的解释器或编译器。

Python是一种语言。 CPython是Python在c语言中的实现,Jython是Java语言中的实现,等等。

总而言之:您已经在使用CPython(如果从这里下载的话)。