Why is it impossible to create a compiler than can compile Python to machinecode like C?
nobody at nowhere.com
Thu Feb 28 23:01:33 CET 2013
On Thu, 28 Feb 2013 12:25:07 -0800, kramer65 wrote:
> 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?
It's not impossible, it's just pointless.
Because Python is dynamically-typed and late-bound, practically nothing is
fixed at compile time. So a compiled Python program would just be a
sequence of calls to interpreter functions.
> Where is my reasoning wrong here? Is that because Python is dynamically
> typed? Does machinecode always need to know whether a variable is an int
> or a float?
> And if so, can't you build a compiler which creates
> machinecode that can handle both ints and floats in case of doubt?
Yes. But it's not just ints and floats. E.g. Python's "+" operator works
on any pair of objects provided that either the left-hand operand has an
__add__ method or the right-hand operand has a __radd__ method.
> Or is it actually possible to do, but so much work that nobody does it?
It's not that it's "so much work" as much as the fact that the resulting
executable wouldn't be any faster than using the interpreter. IOW, it's so
much work for little or no gain.
More information about the Python-list