Why python compile the source to bytecode before interpreting? Why python compile the source to bytecode before interpreting? python python

Why python compile the source to bytecode before interpreting?


Nearly no interpreter really interprets code directly, line by line – it's simply too inefficient. Almost all interpreters use some intermediate representation which can be executed easily. Also, small optimizations can be performed on this intermediate code.

Python furthermore stores this code which has a huge advantage for the next time this code gets executed: Python doesn't have to parse the code anymore; parsing is the slowest part in the compile process. Thus, a bytecode representation reduces execution overhead quite substantially.


Because you can compile to a .pyc once and interpret from it many times.

So if you're running a script many times you only have the overhead of parsing the source code once.


Because interpretting from bytecode directly is faster. It avoids the need to do lexing, for one thing.