Python is a high-level language that first appeared in February 1991; 30 years ago and recently released 3.9.6 a few days ago.
CPython is the default and most widely used implementation of the Python language.
CPython is the original Python implementation. It happens to be implemented in C. It compiles your Python code into bytecode (transparently) and interprets that bytecode in an evaluation loop.
CPython is also the first to implement new features; Python-the-language development uses CPython as the base; other implementations follow.
So why is CPython written in C and not Python?
The answer is located in how compilers work. There are two types of compilers:
Self-hosted compilers: These compilers are written in the language they compile, such as the Go compiler.
Source-to-source compilers: These compilers are written in another language that already has a compiler.
If building a new programming language from scratch, you need an executable application to compile your compiler!
You need a compiler to execute anything, so when new languages are developed, they’re often written first in an older, much more established language.
The Python source code goes through the following to generate an executable code :
Step 1: The Python compiler reads a Python source code or instruction. Then it checks the syntax of each line. If it encounters an error, it immediately halts the translation and shows an error message.
Step 2: If there are no errors, the python source code is then the compiler translates into an intermediate language called “Byte code”.
Step 3: Byte code is then sent to the Python Virtual Machine(PVM), the Python interpreter. PVM converts the Python byte code into machine-executable code.
CPython compiles the source code to generate the CPython bytecode i.e. the .pyc file. The CPython bytecode is then interpreted using a CPython interpreter, and the output runs in a CPython virtual machine. The CPython compiler generates the bytecode just once, but the interpreter is called each time the code runs.
Even though the interpreter takes a lot of time but Python uses it to make use of CPython Byte code to run on cross-platform i.e. Linux, Mac, or Windows.
Fig.1. CPython
There is a lot more information out there on the internet obviously. But I hope this article made some difference.
Thank you for reading.
Hit that follow button if you like my articles, thank you.
References:
CPython - Wikipedia
*CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the…*en.wikipedia.org
Python vs CPython*Even I had the same problem understanding how CPython, JPython, IronPython, and PyPy are different. So…*stackoverflow.com
Basic Tutorial - Cython 3.0.0a9 documentation
*As Cython can accept almost any valid python source file, one of the hardest things in getting started is just figuring…*cython.readthedocs.io