/  Compile() method in Python Built in function

Compile() method in Python Built in function

 

Compile method in built in function returns the desired source an object and is ready to be executed.

 

Syntax:

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

 

Parameters

Compile has the following parameters

  • Filename: this is a parameter where the name for the file is given.
  • Source: it has a source which has a normal string or byte string or an AST object.
  • Mode: the mode can be here either eval, exec or single
  1. Eval: it accepts only a single expression
  2. Exec: it accepts functions, statements, class etc.
  3. Single: it consists of a single interactive statement.

 

Example-1:

text = 'a = 15\nb=1587\nadd=a+b\nprint("add =",add)'
x = compile(text, 'sumstring', 'exec')
exec(x)

 

Output:

Compile () method in Python Built in function