Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95789

Re: Why Python is not both an interpreter and a compiler?

Date 2015-08-31 19:59 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: Why Python is not both an interpreter and a compiler?
References <3e541d13-bc86-456c-8590-4ffd1af9cfd0@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.15.1441015211.23514.python-list@python.org> (permalink)

Show all headers | View raw


On 31Aug2015 01:35, Mahan Marwat <mahanmarwat@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.

This is simplistic and misleading.

Compilation is the process of taking a program and converting it into an 
efficient binary form, attaching whatever else is needed (such a references to 
required libraries). Languages like C are usually compiled to machine code, and 
executed directly by hardware. Languages like Python are usually compiled to 
"byte code", effectively a machine code for a logical machine.

When hardware acts on machine code it is said to execute it. When a piece of 
software acts on byte code, it is said to "interpret" it. The main difference 
is the notional level of execution, and the corresponding level of abstraction 
in the machine code: machine code for a hardware CPU is normally very direct 
and contains instructions directly related to the hardware facilities exposed 
by the CPU (registers, memory, etc). Conversely, byte code (machine code for a 
more abstract interpreter) is more abstract: the basic elements will be higher 
level entities like strings or integers , and memory will be described by more 
abstract items like slots in call frames or names to look up in symbol tables.

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

If you compile a C program with "static" linking you will get a much large 
program. Modern machines support dynamic library linking, where the needed 
libraries are attached to the executing program on the fly, largely when the 
program is started. That relies on the libraries being available in the 
filesystem when the program is invoked. By contrast a "staticly" linked program 
will get copies of the libraries installed into the executable when compiled.

>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?

Chris Angelico has described why a Python interpreter is needed at runtime: 
Python can accept arbitrary Python code at runtime, so it needs to be able to 
compile it (to byte code) and interpret that byte code.

>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?

You can go a long way there. There are alternatives to CPython which try to do 
this, such as Cython. A major obstacle to "complete" compilation is that Python 
is a dynamic language. Every variable may be bound to an object of any type at 
any time, so a lot of behaviour is dependent on the current binding, which may 
change.

Languages like C are staticly typed: their operations convert far more directly 
to machine operations (in fact, this is an explicit target of their design).  
Languages like Python aim for a more freindly and flexible programming syntax, 
and some of the price of that is more indirection between "byte code" and the 
underlying machine operations.

Cheers,
Cameron Simpson <cs@zip.com.au>

remember, information is not knowledge,
knowledge is not wisdom
wisdom is not truth
truth is not beauty
beauty is not love
love is not music
music is the best       - frank zappa

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why Python is not both an interpreter and a compiler? Mahan Marwat <mahanmarwat@gmail.com> - 2015-08-31 01:35 -0700
  Re: Why Python is not both an interpreter and a compiler? Chris Angelico <rosuav@gmail.com> - 2015-08-31 18:49 +1000
  Re: Why Python is not both an interpreter and a compiler? Cameron Simpson <cs@zip.com.au> - 2015-08-31 19:59 +1000
  Re: Why Python is not both an interpreter and a compiler? Ben Finney <ben+python@benfinney.id.au> - 2015-08-31 21:41 +1000
    Re: Why Python is not both an interpreter and a compiler? Marko Rauhamaa <marko@pacujo.net> - 2015-08-31 16:39 +0300
      Re: Why Python is not both an interpreter and a compiler? Mahan Marwat <mahanmarwat@gmail.com> - 2015-08-31 10:48 -0700
        Re: Why Python is not both an interpreter and a compiler? Emile van Sebille <emile@fenx.com> - 2015-08-31 10:56 -0700
        Re: Why Python is not both an interpreter and a compiler? Ben Finney <ben+python@benfinney.id.au> - 2015-09-01 09:15 +1000
        Re: Why Python is not both an interpreter and a compiler? Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-09-01 07:45 +0200
          Re: Why Python is not both an interpreter and a compiler? Ian Kelly <ian.g.kelly@gmail.com> - 2015-09-01 11:21 -0600
        Re: Why Python is not both an interpreter and a compiler? Laura Creighton <lac@openend.se> - 2015-09-01 09:51 +0200
        Re: Why Python is not both an interpreter and a compiler? Steven D'Aprano <steve@pearwood.info> - 2015-09-02 01:20 +1000
          Re: Why Python is not both an interpreter and a compiler? Marko Rauhamaa <marko@pacujo.net> - 2015-09-01 19:20 +0300
            Re: Why Python is not both an interpreter and a compiler? Chris Angelico <rosuav@gmail.com> - 2015-09-02 03:06 +1000
            Re: Why Python is not both an interpreter and a compiler? Laura Creighton <lac@openend.se> - 2015-09-01 19:33 +0200
              Re: Why Python is not both an interpreter and a compiler? Marko Rauhamaa <marko@pacujo.net> - 2015-09-01 23:08 +0300
                Re: Why Python is not both an interpreter and a compiler? Chris Angelico <rosuav@gmail.com> - 2015-09-02 10:50 +1000
                Re: Why Python is not both an interpreter and a compiler? Laura Creighton <lac@openend.se> - 2015-09-02 09:09 +0200
                Re: Why Python is not both an interpreter and a compiler? wxjmfauth@gmail.com - 2015-09-02 01:25 -0700
            Re: Why Python is not both an interpreter and a compiler? Laura Creighton <lac@openend.se> - 2015-09-01 19:52 +0200
            Re: Why Python is not both an interpreter and a compiler? Steven D'Aprano <steve@pearwood.info> - 2015-09-02 12:49 +1000
              Re: Why Python is not both an interpreter and a compiler? Marko Rauhamaa <marko@pacujo.net> - 2015-09-02 06:58 +0300
              Re: Why Python is not both an interpreter and a compiler? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-09-02 08:59 +0300
  Re: Why Python is not both an interpreter and a compiler? Michael Torrie <torriem@gmail.com> - 2015-09-01 21:46 -0600

csiph-web