I was trying to figure out exactly how Python 3 (using CPython as an interpreter) executes its program. I found out that the steps are:
Compilation of Python so
Python is built around the philosophy of duck typing. No explicit type checking takes place, not even during runtime. For example,
>>> x = 5
>>> y = "5"
>>> '__mul__' in dir(x)
>>> True
>>> '__mul__' in dir(y)
>>> True
>>> type(x)
>>>
>>> type(y)
>>>
>>> type(x*y)
>>>
The CPython interpreter checks if x
and y
have the __mul__
method defined, and tries to "make it work" and return a result. Also, Python bytecode never gets translated to machine code. It gets executed inside the CPython interpreter. One major difference between the JVM and the CPython virtual machine is that the JVM can compile Java bytecode to machine code for performance gains whenever it wants to (JIT compilation), whereas the CPython VM only runs bytecode just as it is.