Hence: What is the difference between eval, exec and compilation in Python? :
compile is a lower level version of exec and eval . It does not execute or evaluate your statements or expressions, but returns a code object that can do this. The modes are as follows:
compile(string, '', 'eval') returns a code object that would have been executed if you had done eval(string) . Note that you cannot use statements in this mode; only one (one) expression is valid.compile(string, '', 'exec') returns a code object that would be executed if you did exec(string) . Here you can use any number of operators.compile(string, '', 'single') is similar to exec mode, but it will ignore everything except the first statement. Note that the if / else with its results is considered one of the statements.
UPDATE:
When to compile Python?
Typically, you will compile Python to take advantage of performance. Compiled code has a much faster startup time because it does not need to be compiled, but it does not work faster .
In particular, you would use compile if you want to convert the code to bytecode manually . This raises another important, but lokalny question , why does it?
As stated in a great article :
if you want to use exec and you plan to execute this code more than once make sure that you first compile it into bytecode and then execute that bytecode only and only in the new dictionary as a namespace.
Of particular note are the following:
Now, how much faster is the bytecode executed when creating the bytecode and doing this ?:
$ python -mtimeit -s 'code = "a = 2; b = 3; c = a * b"' 'exec code' 10000 cycles, best 3: 22.7 ms per cycle
$ python -mtimeit -s' code = compile ("a = 2; b = 3; c = a * b",
"," exec ") '' exec code '1,000,000 cycles, best of 3: 0.765 usec per cycle
32 times faster for very short code. It gets a lot worse the more code you have. Why is this so? Since parsing Python code and converting to Bytecode is an expensive operation compared to evaluating bytecode. This, of course, also affects the execfile , which does not completely use bytecode caches to do this. It will not magically check if there is a .pyc file if you are passing the path to the foo.py file.