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

CM cmpython at gmail.com
Mon Mar 4 23:55:06 CET 2013


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



More information about the Python-list mailing list