Python compile() Function
Last Updated :
02 Nov, 2023
Python is a high-level, general-purpose, and very popular programming language. In this article, we will learn about the Python compile() function.
Python compile() Function Syntax
Python compile() function takes source code as input and returns a code object that is ready to be executed and which can later be executed by the exec() function.
Syntax compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
Parameters:
- Source - It can be a normal string, a byte string, or an AST object
- Filename -This is the file from which the code was read. If it wasn't read from a file, you can give a name yourself.
- Mode - Mode can be exec, eval or single.
- a. eval - If the source is a single expression.
- b. exec - It can take a block of a code that has Python statements, class and functions and so on.
- c. single - It is used if consists of a single interactive statement
- Flags (optional) and dont_inherit (optional) - Default value=0. It takes care that which future statements affect the compilation of the source.
- Optimize (optional) - It tells optimization level of compiler. Default value -1.
Python compile() function Example
Converting String to Python Code Object
Example 1: Here filename is mulstring and exec mode allows the use of exec() method and the compile method converts the string to a Python code object.
Python3
srcCode = 'x = 10\ny = 20\nmul = x * y\nprint("mul =", mul)'
execCode = compile(srcCode, 'mulstring', 'exec')
exec(execCode)
Output:
mul = 200
Example 2: In the code we are using compile()
to compile a single expression 'x'
and then execute it using exec()
.
Python3
x = 50
a = compile('x', 'test', 'single')
print(type(a))
exec(a)
Output:
<class 'code'>
50
Python Compile function from File
In this example, we will take main.py file with some string display methods, and then we read the file content and compile it to code the object and execute it.
Python3
String = "Welcome to Geeksforgeeks"
print(String)
Here we will read the file content as a string and then compile it to a code object.
Python3
f = open('main.py', 'r')
temp = f.read()
f.close()
code = compile(temp, 'main.py', 'exec')
exec(code)
Output:
Welcome to Geeksforgeeks
Compile() with eval()
Here eval is used when the source is a single expression.
Python3
x = 50
# Note eval is used for statement
a = compile('x == 50', '', 'eval')
print(eval(a))
Output:
True
Applications
- If the Python code is in string form or is an AST object, and you want to change it to a code object, then you can use compile() method.
- The code object returned by the compile() method can later be called using methods like: exec() and eval() which will execute dynamically generated Python code.
Similar Reads
re.compile() in Python The re.compile() method in Python is used to compile a regular expression pattern into a regex object. Compiling a pattern makes it more efficient when we need to use the same pattern several times, as it avoids re-compiling the pattern each time. Letâs look at how re.compile() works and when we sho
3 min read
Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
9 min read
dir() function in Python The dir() function is a built-in Python tool used to list the attributes (like methods, variables, etc.) of an object. It helps inspect modules, classes, functions, and even user-defined objects during development and debugging.Syntaxdir([object])Parameters: object (optional): Any Python object (lik
3 min read
Python Built in Functions Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
Python - re.compile() In Python, re.compile() from the re module creates a regex object from a regular expression pattern. This object lets you perform operations like search(), match(), and findall(). In this article, we will understand about re.compile() method.Pythonimport re # Compile the pattern pattern = re.compile
2 min read
Python Main Function Main function is like the entry point of a program. However, Python interpreter runs the code right from the first line. The execution of the code starts from the starting line and goes line by line. It does not matter where the main function is present or it is present or not. Since there is no mai
5 min read