Site icon i2tutorials

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

  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:

Exit mobile version