简体   繁体   中英

What's preventing python from being compiled?

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.

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.

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.

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).

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM