Is Python interpreted or compiled or both?

First off, interpreted/compiled is not a property of the language but a property of the implementation. For most languages, most if not all implementations fall in one category, so one might save a few words saying the language is interpreted/compiled too, but it's still an important distinction, both because it aids understanding and because there are quite a few languages with usable implementations of both kinds (mostly in the realm of functional languages, see Haskell and ML). For example, there are C interpreters and tehre are projects that attempt to compile Python (a subset of it, at least) to C or C++ code to compile that to machine code.

First off, interpreted/compiled is not a property of the language but a property of the implementation. For most languages, most if not all implementations fall in one category, so one might save a few words saying the language is interpreted/compiled too, but it's still an important distinction, both because it aids understanding and because there are quite a few languages with usable implementations of both kinds (mostly in the realm of functional languages, see Haskell and ML). For example, there are C interpreters and tehre are projects that attempt to compile Python (a subset of it, at least) to C or C++ code to compile that to machine code.

Second, compilation is not restricted to ahead-of-time compilation to native machine code. A compiler is, more generally, a program that converts a program in one programming language into a program in another programming language (arguably, you can even have a compiler with the same input and output language if significant transformations are applied). And JIT-compilers compile to native machine code at runtime, which can give speed very close or even better (depending on the benchmark and the quality of the implementations compared) than native code.

But to stop nitpicking and answer the question you meant to ask: Practically (read: using a somewhat popular and mature implementation), Python is compiled. Not compiled to machine code ahead of time (i.e. "compiled" by the restricted and wrong, but alas common definition), "only" compiled to to bytecode, but it's still compilation with at least some of the benefits.

For example, the statement a = b.c() is compiled to a byte stream which, when "disassembled", looks somewhat like load 'b'; load_str 'c'; get_attr; call_function 0; store 'a'. This is a simplification, it's actually less readable and a bit more low-level - you can experiment with the standard library dis module and see what the real deal looks like. Interpreting this is faster than trying to interpret from a more high-level representation.

That bytecode is either interpreted (note that there's a difference, both in theory and in practical performance, between interpreting directly and first compiling to some intermediate representation and interpret that), as with the reference implementation (CPython), or both interpreted and compiled to optimized machine code at runtime, as with PyPy.

Alright, this means that a python script is first compiled to bytecode and then it is implemented by an interpreter like CPython, Jython or IronPython etc. – Pankaj Upadhyay Jul 31 '11 at 13:54 @Pankaj: Indeed. In principle, Python is as much of a compiled language as Java and the various . NET languages ;) (Especially since PyPy provides a practically useful JIT-compiler.

) – delnan Jul 31 '11 at 13:56 2 No, it is compiled to bytecode and then the bytecode is executed by the respective VM. CPython is both the compiler and the VM, but Jython and IronPython are just the compiler. – Ignacio Vazquez-Abrams Jul 31 '11 at 13:57 1 @Igacio: I don't have much experience with IronPython/Jython, but doesn't at least Jython provide an interpreter-like layer?

I don't believe it's feasible to try turn Python into the statically-typed JVM bytecode. Still, good point about compiler and interpreter being part of the same package. – delnan Jul 31 '11 at 14:00 @delnan : Thanks mate, it's because of guys like you and skeet, the first choice forum is SO.

– Pankaj Upadhyay Jul 31 '11 at 14:00.

The CPU can only understand machine code indeed. For interpreted program, the ultimate goal of an interpreter is to "interpret" the program code into machine code. However, usually a modern interpreted language does not interpret human code directly because it is too inefficient.

The Python interpreter first read the human code and optimize it to some immediate code before interpreting it into machine code. That's why you always need another program to run a Python script unlike in C++ you can run the executable directly. For example c:\Python27\python.

Exe or /usr/bin/python.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions