Why is it impossible to create a compiler than can compile Python to machinecode like C?

Stefan Behnel stefan_ml at behnel.de
Thu Feb 28 16:03:09 EST 2013


kramer65, 28.02.2013 21:25:
> I'm using Python for a while now and I love it. There is just one thing
> I cannot understand. There are compilers for languages like C and C++.
> why is it impossible to create a compiler that can compile Python code
> to machinecode?

All projects that implement such compilers prove that it's quite possible.

The most widely used static Python compiler is Cython, but there are also a
couple of experimental compilers that do similar things in more or less
useful or usable ways. And there are also a couple of projects that do
dynamic runtime compilation, most notably PyPy and Numba.

You may want to take a look at the Python implementations page,
specifically the list of Python compilers:

http://wiki.python.org/moin/PythonImplementations#Compilers


> Does machinecode always need to know whether a variable is an int or a 
> float?

Not at all. You're mixing different levels of abstraction here.


> And if so, can't you build a compiler which creates machinecode 
> that can handle both ints and floats in case of doubt?

Sure. Cython does just that, for example, unless you tell it explicitly to
restrict a variable to a specific type. Basically, you get Python semantics
by default and C semantics if you want to.

Stefan




More information about the Python-list mailing list