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?
-
13What do you mean by "treated as only an interpreted language"? Usually I see python referred to as a scripting language. Personally, I don't think the terms "compiled" and "interpreted" are that useful these days because the line between them has gotten so blurry.– user53141Commented Nov 28, 2014 at 3:15
-
2Hi New user - you should probably check out stackoverflow.com/questions/6889747/… and stackoverflow.com/questions/745743/…– James SnellCommented Nov 28, 2014 at 8:14
-
See also: Is Python Interpreted or Compiled? and Interpreted vs Compiled: A useful distinction?– user40980Commented Nov 29, 2014 at 1:06
6 Answers
Is Python interpreted (like Javascript or PHP)? is worth a read for more information and it says:
...different implementations of languages may do different things. These days you can find both C interpreters and Javascript compilers.
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.
-
There's also bytecode compilation with compilation to machine code once at install time, e.g. LLVM and Android Runtime.– DovalCommented Nov 28, 2014 at 14:18
-
-
1I like this answer for the 'levels' of compiled-ness. +1! Commented Nov 29, 2014 at 6:05
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.
-
2"A C compiler does all three" - what is the byte code here? I thought
.obj
files were just unlinked chunks of the target machine code plus some metadata on how to link them? Commented Nov 28, 2014 at 9:11 -
Despite that Java bytecode is "very hard" to decompile, there are free decompilers (or at least one) that can produce a .java source code nearly identical to the source file (minus comments and whitespace). It's much harder in C because the compiler does much more optimisations than a java compiler can. Commented Nov 28, 2014 at 9:13
-
2@JanDvorak - most compilers use an intermediate language internally; there's probably an option to write this out to a file. You're right, despite the difficulty, there are some good Java decompilers. Be aware they use heuristics and struggle with some constructs, e.g. distinguishing between for and while loops. Reversing steps 1 and 3 usually involves no heuristics.– paj28Commented Nov 28, 2014 at 9:16
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.
-
1"They sometimes change it from version to version" could be said of every platform architecture, including JVM. JSR 292 added bytecode in ~2008. Hardware processors do the same. POWER and x86, e.g., add (and occasionally, retire) instructions every few years, and have done so since their inception. It does not make code compiled for those targets "not compiled." Commented Nov 29, 2014 at 1:58
With python
, you can enter a line of code into python and expect it to be interpreted and executed immediately.
Compiled languages like c
, 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:
print "Hello"
-
1
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.