views:

328

answers:

2

I don't really get the concept of "bytecode interpreter" in the context of CPython. Can someone shed some light over the whole picture?

Does it mean that CPython will compile and execute pyc file (bytecode file?). Then what compile py file to pyc file? And how is Jython different from CPython (except they are implemented in different languages).

I also read somewhere that Python is C++ interpretation. Is this correct? And what does that mean?

I'm still very new to Python, so forgive me if I ask the dumb questions... Thank you so much!

+4  A: 

CPython is both the bytecode compiler, and interpreter (virtual machine).

It automatically detects if no existing pre-compiler file (.pyc) exists, compiles the code, and saves it out.

unwind
+6  A: 

CPython is the implementation of Python in C. It's the first implementation, and still the main one that people mean when they talk about Python. It compiles .py files to .pyc files. .pyc files contain bytecodes. The CPython implementation also interprets those bytecodes. CPython is not written in C++, it is C.

The compilation from .py to .pyc happens transparently as needed. When you execute a .py file, it will first be compiled to a .pyc file if needed, then the .pyc file will be interpreted.

Jython is different because (in addition to being implemented in Java instead of C) it compiles .py files into .class files so they can be executed in the JVM.

Ned Batchelder
Does Jython convert the Python code to Java and then compile to Jave bytecode?
Casey
I don't know if it produces Java as an intermediate form.
Ned Batchelder
AFAIK Jython can produce Java as an intermediate form if requested, but it usually just produces Java bytecode.
MAK