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

Matty Sarro msarro at gmail.com
Thu Feb 28 15:50:00 EST 2013


Python is an interpreted language, not a compiled language. This is
actually a good thing! What it means is that there is a "scripting engine"
(we just call it the interpreter) that actually executes everything for
you. That means that any operating system that has an interpreter written
for it is capable of running the exact same code (there are lots of
exceptions to this, but in general it is true). It makes code much more
portable. Also, it makes it easy to troubleshoot (compiled programs are a
pain in the butt unless you add additional debugging elements to them).

A compiled program on the other hand must be specifically compiled for the
destination architecture (so if you're trying to write an OSX executable on
windows, you need a compiler capable of doing that). So doing any sort of
cross platform development can take significantly longer. Plus then, as I
said, debugging will require additional debug tracing elements to be added
to the code you write. The benefit though is that compilers can optimize
code for you when they compile, and the compiled code will tend to run
faster since you're not dealing with an interpreter between you and the
machine.

Now, there are places where this line is blurred. For instance perl is an
interpreted language, but capable of running EXTREMELY fast. Python is a
little slower, but significantly easier to read and write than perl. You
also have some weird ones like JAVA which actually have a virtual machine,
and "half compile" source code into java "bytecode." This is then executed
by the virtual machine.

I guess the ultimate point is that they're all designed for different
purposes, and to solve different problems. Python was intended to make
fast-to-write, easily understandable, easily portable code which can be
executed on any system which has the Python interpreter. It's not really
intended for things which require lower level access to hardware. It's what
we call a "high level" programming language.

C (your example) was intended for very low level programming, things like
operating systems, device drivers, networking stacks, where the speed of a
compiled executable and direct access to hardware was a necessity. That's
what Dennis Ritchie wrote it for. We call it a "mid level" programming
language, or a "low level" programming language depending on who you talk
to. I'd have to say mid level because low level would be writing in
assembly or playing with a hex editor :)

Different tools for different jobs.

HTH.

-Matty


On Thu, Feb 28, 2013 at 3:25 PM, kramer65 <kramerh at gmail.com> wrote:

> Hello,
>
> 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?
>
> My reasoning is as follows:
> When GCC compiles a program written in C++, it simply takes that code and
> decides what instructions that would mean for the computer's hardware. What
> does the CPU need to do, what does the memory need to remember, etc. etc.
> If you can create this machinecode from C++, then I would suspect that it
> should also be possible to do this (without a C-step in between) for
> programs written in Python.
>
> 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? Or is it actually
> possible to do, but so much work that nobody does it?
>
> I googled around, and I *think* it is because of the dynamic typing, but I
> really don't understand why this would be an issue..
>
> Any insights on this would be highly appreciated!
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130228/38a32b7b/attachment.html>


More information about the Python-list mailing list