Why is that python is treated as only an interpreted language when there is in fact a compiling stage where the code we write gets converted into byte code(.pyc), which is later interpreted by the PVM. Is it wrong to say python is just an interpreted language?
Compiled and interpreted are descriptions of an implementation, not of a language; Their usage in relation to languages is a throwback to the days when each language had few implementations and similar (simple) toolchains compared to the kinds of functions you might find in an IDE today.
There are layers of interpretation/compilation...
- Pure interpretation
- Tokenisation + interpretation
- Bytecode compilation which requires a virtual machine to host
- Bytecode + 'just in time' compilation to Machine code
- Native Machine code compilation
Different Python environments use different options,
cpython does only a bytecode compilation, where
Psyco uses a bytecode + JIT approach.
New languages and implementations frequently use the Bytecode + JIT approach as it is the best way to make them portable and enables the application to make use of more features of an unknown host environment.
In terms of the language itself the descriptions are used as a shorthand for the level of abstraction. With high-level languages like Python where performance consideration and optimisation is far removed from the programmers remit often described as being interpreted, where low level ones like C where those considerations are closely tied are considered compiled.
To answer to your question - Python as most people use it is PARTIALLY COMPILED and it would be right to consider it BOTH compiled AND interpreted.
This is a bit of a simplification, but there are three main steps in compiling code:
- Parse the source code to a syntax tree
- Compile the syntax tree into machine-independent byte code
- Translate byte code to machine code
A C compiler does all three, and is without doubt a compiler. A Java compiler does 1 and 2, producing .class files, and the runtime does 3. The Python "compiler" only does step 1 when producing .pyc files, while the runtime does 2 and 3.
So, as Bart says, the difference between "compiled" and "interpreted" is somewhat subjective. But step 2 is a very significant step, and it is the most difficult to do in reverse, i.e. decompile. So that's why Java is usually called compiled and Python usually called interpreted.
The above is my original post. Since posting I've read more about .pyc files and need to correct it. The Python compiler does actually do step 2; the contents of pyc files is a stack-based intermediate language, quite similar to Java class files.
I wonder if the reason that Python is considered "interpreted" is because there's a single binary to compile and execute.
Unlike .NET and Java, there is not a well-defined standard for python byte code, and they sometimes change it from version to version. See also this question.
This has the implication that you can't really distribute the compiled version of your code, which in turn means, that you can't really consider it a compiled language.
python, you can enter a line of code into python and expect it to be interpreted and executed immediately.
Compiled languages like
java, etc. require a compilation step which render an object that may be executable. There may be additional steps require to make the code executable.
Unlike an compiled language, an interpreted language can run a command like:
There is no clear distinction anymore between compiled and interpreted languages any more, as there are very few languages left that are purely interpreted.
It seems like that the term 'interpreted language' is actually being used for scripting languages, where you can work in a tight "modify - execute" cycle without an explicit compilation step in the workflow.
You can say that when we're talking about Python as a whole; including compiler that creates byte code and a virtual machine that interprets the byte code. Python as an language has no saying about if it's an compiled or interpreted programming language, only the implementation of it. Often with semantic issues, there are programming languages where the user can choose to compile the code into byte code to be interpreted at run time or compile it directly into machine code.