Cpython

 Cpython
Written by Sagar RabidasNovember 27, 2021
12 min read
Python
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Sagar Rabidas

Software Developer

Now we know in this blog CPython.

So what is CPython?

CPython is the authentic python implementation. It's miles the implementation you download from python.Org. Human beings name it CPython to distinguish it from other, later, python implementations, and to differentiate the implementation of the language engine from the python programming language itself.

CPython happens to be applied in c. This is just an implementation detail. CPython compiles your python code into bytecode (transparently) and interprets that bytecode in an evaluation loop.

CPython is likewise the first to put in force new capabilities; python-the-language development makes use of CPython because the base; different implementations observe.

Compiling to C

So CPython does no longer translate your python code to c through itself. Instead, it runs an interpreter loop. There may be an assignment that does translate python-ish code to c, and this is called CPython. Cython provides a few extensions to the python language and helps you to compile your code to c extensions, code that plugs into the CPython interpreter.

In line with Wikipedia, "A programming language is a notation for writing applications, which are specifications of a computation or algorithm". This means that it is in reality the rules and syntax for writing code. Separately we've got a programming language implementation which in most instances, is the actual interpreter or compiler.

Python is a language. CPython is the implementation of Python in C. Jython is the implementation in Java.

If you want to look at CPython's bytecode then you can. Here's how you can:

>>> 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 function definitions. 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).

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.

python
cpython
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan