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

88888 Dihedral dihedral88888 at googlemail.com
Mon Mar 4 18:12:07 EST 2013


On Tuesday, March 5, 2013 6:55:06 AM UTC+8, CM 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.
> 
> 
> 
> Thanks.

The dynamic type part is normally in the higher level components of 
objects and functions and generators.

Of course if one can be sure of the types of variables used 
in some functions then that is the cython way to speed up pure OOP python
programs in executions.






More information about the Python-list mailing list