Python

Python Menu

Python

Python compile() Function - Definition, Syntax, Parameters, Examples

Definition

The compile() function returns the specified source as a code object, ready to be executed.

Syntax

compile(source, filename, mode, flag, dont_inherit, optimize)

Parameters

Parameter Description
source Required. The source to compile, can be a String, a Bytes object, or an AST object
filename Required. The name of the file that the source comes from. If the source does not come from a file, you can write whatever you like
mode Required. Legal values:
'eval' - if the source is a single expression
'exec' - if the source is a block of statements
'single' - if the source is a single interactive statement
flag Optional. How to compile the source. Default 0
dont_inherit Optional. How to compile the source. Default False
optimize Optional. Defines the optimization level of the compiler. Default -1

Examples

result = compile('print(78)\nprint(23)', 'test', 'exec') exec(result)

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods