In this tutorial we will learn about the python compile() method and its uses.
The compile() method in python code objects from the source can be a normal string, a byte string, or an AST object.
The syntax of compile() is:
Python compile() Method Parameters
- source – A simple string, a byte string, or an AST object.
- filename – The name of the file from the code comes from. If the code does not come from a file you can write anything you like.
- mode – Legal values can be eval, or exec, or single.
- eval – Accepts only a single expression.
- exec – It can take a code block with a python statement, class, and methods, etc.
- single – if it consists of a single interactive statement.
- flags – How to compile the source. The default will be 0.
- don’t-inherit – How to compile the source. Default False
- optimize – Defines the optimization level of the compiler. Default -1
The compile() method is used if the python code is in simple string form or is an AST object, and if you want to change it to a code object.
Let us see an example of the usage of compile() method.
Example 1: How to use compile() method?
Output will be as follow:
Here we are taking a simple string as a source. And the filename is sumstring. After that, we are taking exec mode that allows us the usage of exec() method.
As we can see, it showing class code when we are checking its type.
In simple words compile() method can convert the simple string to a python code object. The code object can be executed using the exec() method.
Example 2: How to use compile() method with eval()?
Let see an example to use the compile() method to execute the code using the eval() method.
The output will be as follow:
Example 3: How to use compile() method byte string source?
Let take an example of using byte string as source in compile() method.
Rules of compile() method
compile() method returns objects as python code object.