13

I understand that Python is an interpreted language, but the performance would be much higher if it was compiled.

  • What exactly is preventing python from being compiled?
  • Why was python designed as an interpreted language and not a compiled one in the first place?

Note: I know about .pyc files, but those are bytecode, not compiled files.

aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
NerdOfLinux
  • 1,017
  • 10
  • 24
  • 2
    Certainly. Write a Python compiler and linker, plus the libraries that it would need to use, and you'll be all set. There's no way to *capture that machine code* because it doesn't exist. The byte code is executed by the interpreter. That *machine code* is in the interpreter. – Ken White Aug 29 '17 at 01:40
  • *Compiled* bytecode, which is ran through an interpreter because Python is an interpreted language. – OneCricketeer Aug 29 '17 at 01:40
  • Python is a compiled language. It just isn't compiled to a language for which a hardware implementation exists; it is executed by a virtual machine. Compilation and interpreters aren't mutually exclusive. – chepner Aug 29 '17 at 01:42
  • 6
    Languages are neither compiled nor interpreted. Language *implementations* are compiled or interpreted. – Russell Borogove Aug 29 '17 at 01:43
  • Possible duplicate of [If Python is interpreted, what are .pyc files?](https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files) – halfer Aug 29 '17 at 07:56
  • (What Russell Borogove said is correct. *I* should have said that the reference implementation compiles Python source to Python byte code, which is interpreted by a virtual machine.) – chepner Aug 29 '17 at 11:18

4 Answers4

13

Python, the language, like any programming language, is not in itself compiled or interpreted. The standard Python implementation, called CPython, compiles Python source to bytecode automatically and executes that via a virtual machine, which is not what is usually meant by "interpreted".

There are implementations of Python which compile to native code. For example, the PyPy project uses JIT compilation to get the benefits of CPython's ease of use combined with native code performance.

Cython is another hybrid approach, generating and compiling C code on the fly from a dialect of Python.

However, because Python is dynamically typed, it's not generally practical to completely precompile all possible code paths, and it won't ever be as fast as mainstream statically typed languages, even if JIT-compiled.

Russell Borogove
  • 18,516
  • 4
  • 43
  • 50
0

Python is a scripting language, often used for things like rapid prototyping or fast development, so I guess the thought process behind interpretor over compiler is that it simplifies things for the programmer in those domains (at the cost of performance). Nothing is stopping you or others from writing compilers for Python however; Facebook did something like this for PHP when they wrote HHVM to execute the bytecode of compiled Hack (their typed variant of PHP).

In fact, there are project(s) out there that do just that with python. Cython is one example I can think of off the top of my head (cython.org).

0

I think python code can be compiled to some extent but we are unable to compile everything in python before hand. This is due to the loosely typed style of python where you can change variable type anywhere in the program. A modification of Python namely Rpython has much strict style and hence can be compiled completely.

Daniyal Ahmed
  • 715
  • 5
  • 11
0

Python is language which primarily built for writing readable and expressive code. Python wraps many features from all it's neighbours.

Let's see why we don't need python code to be compiled into assembly or machine. Now let's compare a native language with python. Let's take C++. There are some python specific features like you don't have to do any type declaration in python. This managed by Python interpreter. But if you try to implement same feature in C++ it's a burden over compiler. It will add code to check a variable's type every time before it's getting accessed for any purpose. Even python compiler does the same operation. It means that you are not improving runtime performance at all.

And most of the python functions are c functions which python compiler internally calling when we call it in python script.

The primary reason we don't need a python compiler is that it doesn't improve performance in a large scale. It's waste to write a software which increases risk than reducing it. And python is damn fast once all of it's code in main memory.

not_python
  • 904
  • 6
  • 13