Python built-in function "compile". What is it used for?

31,757

Solution 1

It is not that commonly used. It is used when you have Python source code in string form, and you want to make it into a Python code object that you can keep and use. Here's a trivial example:

>>> codeobj = compile('x = 2\nprint "X is", x', 'fakemodule', 'exec')
>>> exec(codeobj)
X is 2

Basically, the code object converts a string into an object that you can later call exec on to run the source code in the string. (This is for "exec" mode; the "eval" mode allows use of eval instead, if the string contains code for a single expression.) This is not a common task, which is why you may never run across a need for it.

The main use for it is in metaprogramming or embedding situations. For instance, if you have a Python program that allows users to script its behavior with custom Python code, you might use compile and exec to store and execute these user-defined scripts.

Another reason compile is rarely used is that, like exec, eval, and their ilk, compile is a potential security hole. If you take user code in string form and compile it and later exec it, you could be running unsafe code. (For instance, imagine that in my example above the code was formatYourHardDrive() instead of print x.)

Solution 2

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 it. The modes are as follows:

  1. compile(string, '', 'eval') returns the code object that would have been executed had you done eval(string). Note that you cannot use statements in this mode; only a (single) expression is valid. Used for a single expression.
  2. compile(string, '', 'exec') returns the code object that would have been executed had you done exec(string). You can use any number of statements here. Used for an entire module.
  3. compile(string, '', 'single') is like the exec mode, but it will ignore everything except for the first statement. Note that an if/else statement with its results is considered a single statement. Used for one single statement.

Take a look that the documentation. There is also an awesome (well, dumbed down) explanation at http://joequery.me/code/python-builtin-functions/#compile with an excellent example of usage.

Solution 3

What specifically don't you understand? The documentation explains that it will:

Compile the source into a code or AST object. Code objects can be executed by an exec statement or evaluated by a call to eval(). source can either be a Unicode string, a Latin-1 encoded string or an AST object. Refer to the ast module documentation for information on how to work with AST objects.

So it takes python code, and returns on of those two things

  • exec will execute the python code
  • eval will evaluate an expression, which is less functional than exec
  • ast allows you to navigate the Abstract Syntax Tree that the code generates
Share:
31,757

Related videos on Youtube

demo.b
Author by

demo.b

C#, Java, Python...

Updated on December 08, 2020

Comments

  • demo.b
    demo.b over 3 years

    I came across a built-in function compile today. Though i read the documentation but still do not understand it's usage or where it is applicable. Please can anyone explain with example the use of this function. I will really appreciate examples.

    From the documentation, the function takes some parameters as shown below.

    compile(source, filename, mode[, flags[, dont_inherit]])
    
  • JoeQuery
    JoeQuery about 10 years
    I preferred it when you called my explanation awesome ;)
  • anon582847382
    anon582847382 about 10 years
    @JoeQuery Wow, hello! I have learnt loads from your stuff since I started! Thank you!
  • eatonphil
    eatonphil almost 10 years
    +1 for bringing up ASTs - what compile is actually used for [1]. [1] - svn.python.org/view/python/trunk/Demo/parser/…
  • x29a
    x29a over 9 years
    see also the accepted answer from @max-shawabkeh here stackoverflow.com/questions/2220699/…
  • Mahesha999
    Mahesha999 about 6 years
    what if I have strings of two files: py1.py and py2.py (consider I get those strings from database cells, uploaded by user by selecting those files in browser interface), wherein py1.py calls function written in py2.py. Is there any way I can handle this?
  • BrenBarn
    BrenBarn about 6 years
    @Mahesha999: There might be a way, but not a simple way. That's definitely out of scope for a comment here. You could try asking it as a separate question.
  • Davos
    Davos about 6 years
    dumbed down explanations are awesome