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


Groups > comp.lang.python > #40489

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

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'interpreter': 0.04; 'subject:Python': 0.05; 'compiler': 0.05; 'run-time': 0.05; 'that?': 0.05; 'c++,': 0.07; 'function,': 0.07; 'line:': 0.07; 'referring': 0.07; 'python': 0.09; '"run': 0.09; 'compilers.': 0.09; 'objects.': 0.09; 'overridden': 0.09; 'referenced': 0.09; 'runtime': 0.09; 'subclass': 0.09; 'subject:Why': 0.09; 'subject:create': 0.09; 'variables,': 0.09; 'looked': 0.10; 'language,': 0.11; 'language': 0.14; 'better?': 0.16; 'compilers': 0.16; 'degree.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers,': 0.16; 'lookups.': 0.16; 'objects?': 0.16; 'pyobject': 0.16; 'subject: \n ': 0.16; 'time"': 0.16; 'later': 0.16; 'string': 0.17; 'wrote:': 0.17; 'integer': 0.17; 'string,': 0.17; 'typing': 0.17; '(in': 0.18; '(or': 0.18; 'appropriate': 0.20; 'code.': 0.20; 'sort': 0.21; 'back.': 0.22; 'defined': 0.22; "i've": 0.23; 'machine': 0.24; 'least': 0.25; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'compiled': 0.27; 'possibly': 0.27; 'c++': 0.27; 'object,': 0.27; 'possible,': 0.27; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'run': 0.28; 'subject:like': 0.29; 'time;': 0.29; 'class': 0.29; 'knows': 0.30; 'function': 0.30; 'figure': 0.30; 'up.': 0.31; 'code': 0.31; 'point': 0.31; 'instruction': 0.32; 'could': 0.32; 'doubt': 0.33; 'function.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'times.': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'done': 0.34; 'whatever': 0.35; 'adds': 0.35; 'stores': 0.35; 'so,': 0.35; 'received:209.85.220': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'really': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'enough': 0.36; 'two': 0.37; 'why': 0.37; 'virtual': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'things': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'help': 0.40; 'most': 0.61; 'matter': 0.61; 'time,': 0.62; 'is.': 0.62; 'different': 0.63; 'more': 0.63; 'become': 0.65; '2013': 0.84; 'dict,': 0.84; 'seen.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=r/e5bnCvE+VeHKryBDwB0aomwn1hCi56PNpu27O8R/4=; b=dM4y3xLt81MnubrbgyKs2TWpvBn9dNIqmPiYB4UcifJVeDy79/yBDw9+TIYAlaYbDU K5aH9YjxDS2cvZh8Lf8HF7YMau6b2D9Sh8E11di/PyP/jAGbOHOCX3mg9WbBCfkFrYFj 5TBO8h1Vc/ezAVIfErGYWXqK4RRHV450SUF9RKTKmPg/uz1k3znUft0U69h9eZswfHJj 41pp5a5JZQi3q+Y/h4QrZ621Fb3pIZeOGDyV9c5xGMUFUQWCIsQD4/RmVxkIxRi0w3wT MP/gen7EGTngi2u8Rg97J5vkHdU21tB95GTfmEBjzt51KLBWpfB0QJgG+00J01bD2rtG ZcjQ==
MIME-Version 1.0
X-Received by 10.220.223.80 with SMTP id ij16mr8644612vcb.28.1362443627234; Mon, 04 Mar 2013 16:33:47 -0800 (PST)
In-Reply-To <b91a25e1-0d00-4b29-a229-47ad32d9feae@h17g2000yqe.googlegroups.com>
References <b428fdef-a577-45c0-b37c-60bde74e3ae1@googlegroups.com> <kh2iij$msn$1@reader1.panix.com> <b91a25e1-0d00-4b29-a229-47ad32d9feae@h17g2000yqe.googlegroups.com>
Date Tue, 5 Mar 2013 11:33:47 +1100
Subject Re: Why is it impossible to create a compiler than can compile Python to machinecode like C?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2858.1362443630.2939.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362443630 news.xs4all.nl 6869 [2001:888:2000:d::a6]:36208
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40489

Show key headers only | View raw


On Tue, Mar 5, 2013 at 9:55 AM, CM <cmpython@gmail.com> wrote:
>
>> The main issue is that python has dynamic typing.  The type of object
>> that is referenced by a particular name can vary, and there's no way
>> (in general) to know at compile time what the type of object "foo" is.
>>
>> That makes generating object code to manipulate "foo" very difficult.
>
> Could you help me understand this better?  For example, if you
> have this line in the Python program:
>
> foo = 'some text'
> bar = {'apple':'fruit'}
>
> If the interpreter can determine at runtime that foo is a string
> and bar is a dict, why can't the compiler figure that out at
> compile time?  Or is the problem that if later in the program
> you have this line:
>
> foo = 12
>
> now foo is referring to an integer object, not a string, and
> compilers can't have two names referring to two different
> types of objects?  Something like that?
>
> I in no way doubt you that this is not possible, I just don't
> understand enough about how compiling works to yet "get"
> why dynamic typing is a problem for compilers.

Python doesn't have "variables" with "values"; it has names, which may
(or may not) point to objects. Dynamic typing just means that one name
is allowed to point to multiple different types of object at different
times.

The problem with dynamic typing is more one of polymorphism. Take this
expression as an example:

foo += bar;

In C, the compiler knows the data types of the two variables, and can
compile that to the appropriate code. If they're both integers,
that'll possibly become a single machine instruction that adds two
registers and stores the result back.

In C++, foo could be a custom class with an operator+= function. The
compiler will know, however, what function to call; unless it's a
virtual function, in which case there's a run-time check to figure out
what subclass foo is of, and then the function is called dynamically.

In Python, *everything* is a subclass of PyObject, and every function
call is virtual. That += operation is backed by the __iadd__ function,
defined by PyObject and possibly overridden by whatever type foo is.
So, at run time, the exact function is looked up.

C++ is most definitely a compiled language, at least in most
implementations I've seen. But it has the exact same issue as Python
has: true dynamism requires run-time lookups. That's really what
you're seeing here; it's nothing to do with any sort of "compiled" vs
"interpreted" dichotomy, but with "compile time" vs "run time"
lookups. In C, everything can be done at compile time; in Python, most
things are done at run time.

It's mainly a matter of degree. A more dynamic language needs to do
more work at run time.

ChrisA

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


Thread

Why is it impossible to create a compiler than can compile Python to machinecode like C? kramer65 <kramerh@gmail.com> - 2013-02-28 12:25 -0800
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Matty Sarro <msarro@gmail.com> - 2013-02-28 15:50 -0500
    Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 02:55 +0000
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Stefan Behnel <stefan_ml@behnel.de> - 2013-02-28 22:03 +0100
    Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 03:47 +0000
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? alex23 <wuwei23@gmail.com> - 2013-02-28 20:31 -0800
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Stefan Behnel <stefan_ml@behnel.de> - 2013-03-01 08:48 +0100
        Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-02 01:49 +0000
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Chris Angelico <rosuav@gmail.com> - 2013-03-01 08:10 +1100
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Stefan Behnel <stefan_ml@behnel.de> - 2013-02-28 22:17 +0100
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Dave Angel <davea@davea.name> - 2013-02-28 16:18 -0500
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Modulok <modulok@gmail.com> - 2013-02-28 14:19 -0700
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Jonas Geiregat <jonas@geiregat.org> - 2013-02-28 22:33 +0100
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Nobody <nobody@nowhere.com> - 2013-02-28 22:01 +0000
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Terry Reedy <tjreedy@udel.edu> - 2013-02-28 17:06 -0500
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-28 21:09 -0500
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-01 04:27 +0000
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? alex23 <wuwei23@gmail.com> - 2013-02-28 20:38 -0800
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-28 22:21 -0800
  Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Grant Edwards <invalid@invalid.invalid> - 2013-03-04 16:36 +0000
    Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? CM <cmpython@gmail.com> - 2013-03-04 14:55 -0800
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-04 15:12 -0800
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Terry Reedy <tjreedy@udel.edu> - 2013-03-04 19:31 -0500
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Chris Angelico <rosuav@gmail.com> - 2013-03-05 11:33 +1100
      Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Benjamin Kaplan <benjamin.kaplan@case.edu> - 2013-03-04 16:27 -0800
    Re: Why is it impossible to create a compiler than can compile Python to machinecode like C? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-05 01:35 +0000

csiph-web