osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why Python is not both an interpreter and a compiler?


On Mon, Aug 31, 2015 at 6:35 PM, Mahan Marwat <mahanmarwat at gmail.com> wrote:
> What I know about an interpreter and a compiler is: they both convert source code to machine code and the only difference is, an interpreter convert it, line by line while compiler convert the whole source file.
> Now if we compile a C source file on C compiler, it will produce a small executable file. But if we compile (freeze) a Python source file on Python interpreter (using cx_freeze), it will produce a big executable file.
> Now the thing is C compiler does not embed a C compiler inside our program, while the Python interpreter (cx_freeze, pytoexe) embed Python interpreter inside our program, what is the reason?
> The question is, why cx_freeze etc... embed Python interpreter inside our programs, they are unable to produce machine code like C compiler do?
> Cant we program a language, which is both interpreted and compiled? The core developer cant add the compiling functionality to Python?

Compiling is trivially easy for trivial programs, and becomes
virtually impossible for arbitrarily complex and dynamic programs. In
an extreme example, Python includes the ability to execute arbitrary
code:

>>> exec("print('Hello, world!')")
Hello, world!

You basically can't compile that without having the full capabilities
of the Python language. If you want something that makes Python into
an executable binary, you either want a wrapper (like cx_freeze and
pytoexe and so on), or else something like the PyPy project. Check it
out - it's pretty cool!

ChrisA