The compile () function in Python

The compile () function returns a code object in Python from the specified source, be it a normal string, byte string or AST object. How is the syntax of compile () function, what parameters does it have and how to use it? Invites you to read the track.

The compile () function in Python Picture 1

The compile () function syntax in Python

 compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) 

The compile () function uses when your Python code is in the form of strings or AST and you want to convert it to an object code.

The object code converted with compile () will be invoked by the program by exec () and eval ().

The parameters of compile function ()

  1. source: the source you want to convert, usually in the form of a string, byte object or AST object.
  2. filename: file name contains the source. If the source does not belong to any file, you can name it here.
  3. mode: includes the following values:
    1. eval - if the source is an expression.
    2. exec - if the source is a block of statements, classes, and functions.
    3. single - if the source is an interactive statement.
  4. flags: control commands that affect source compilation, default to 0. This parameter is optional.
  5. dont_inherit: control the commands that affect the source compilation, the default is False. This parameter is not required.
  6. optimize: specify the optimization level of the compiler, the default is -1. This parameter is not required.

Return value from compile ()

The compile () function returns a code object in Python.

Example: How does the compile () function work?

Follow the following example:

 codeInString = 'a = 5nb=6nsum=a+bnprint("sum =",sum)' 
codeObejct = compile(codeInString, 'sumstring', 'exec')
 
exec(codeObejct)

Run the program, the result is:

 sum = 11 

In the above example, the source here is a normal string, filename is sumstring, mode is exec after conversion, you can use the exec () function to call the resulting code object.

You can see a list of built-in Python functions and don't forget to do Python exercises to reinforce your knowledge.

Previous lesson: complex () function in Python

Next article: Function delattr () in Python

4 ★ | 1 Vote

May be interested

  • The function dir () in PythonThe function dir () in Python
    the dir () function in python returns a list of valid properties of the object. quantrimang will learn more about this function content through the article. invites you to read the track.
  • The function set () in PythonThe function set () in Python
    in this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
  • Help () function in PythonHelp () function in Python
    the built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
  • Sum () function in PythonSum () function in Python
    the built-in function sum () in python returns the sum of all numbers in iterable.
  • The slice () function in PythonThe slice () function in Python
    the slice () function in python returns a slice object that helps you determine how to cut an existing string.
  • The chr () function in PythonThe chr () function in Python
    in python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
  • The iter () function in PythonThe iter () function in Python
    continuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
  • The pow () function in PythonThe pow () function in Python
    the pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
  • The reversed () function in PythonThe reversed () function in Python
    the reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
  • Exec () function in PythonExec () function in Python
    the exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.