c python-Python vs Cpython

Even I had the same problem understanding how are CPython, JPython, IronPython, PyPy are different from each other.

So, I am willing to clear three things before I begin to explain:

Python: It is a language, it only states/describes how to convey/express yourself to the interpreter (the program which accepts your python code).

Implementation: It is all about how the interpreter was written, specifically, in what language and what it ends up doing.

Bytecode: It is the code that is processed by a program, usually referred to as a virtual machine, rather than by the "real" computer machine, the hardware processor.

CPython is the implementation, which was

written in C language. It ends up producing bytecode (stack-machine

based instruction set) which is Python specific and then executes it.

The reason to convert Python code to a bytecode is because it"s easier to

implement an interpreter if it looks like machine instructions. But,

it isn"t necessary to produce some bytecode prior to execution of the

Python code (but CPython does produce).

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 a function definition. 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.

Here, you may ask if the CPython"s bytecode is portable like Jython, I suspect not. The bytecode produced in CPython implementation was specific to that interpreter for making it easy for further execution of code (I also suspect that, such intermediate bytecode production, just for the ease the of processing is done in many other interpreters).

So, in Jython, when you compile your Python code, you end up with Java byte code, which can be run on a JVM.

Similarly, IronPython (written in C# language) compiles down your Python code to Common Language Runtime (CLR), which is a similar technology as compared to JVM, developed by Microsoft.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值