Remember
compile() turns source text into a code object. Execution is a separate step; the mode must match the kind of source.
compile() turns source text into a code object. Execution is a separate step; the mode must match the kind of source.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
source = "number = 3"
code = compile(source, "sample", "exec")
namespace = {}
exec(code, namespace)
print(namespace["number"])Top-level scope
'number = 3'Object 2 · code<code object <module>, file "sample", line 1>Object 3 · dict{'number': 3}compile() turns source text into a code object. Execution is a separate step; the mode must match the kind of source.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
source = "number = 3"
code = compile(source, "sample", "exec")
namespace = {}
exec(code, namespace)
print(namespace["number"])
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
source --> object 1 (str)
code --> object 2 (code)
namespace --> object 3 (dict)
object 1: 'number = 3'
object 2: <code object <module>, file "sample", line 1>
object 3: {'number': 3}Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
source = "number = 3"
code = compile(source, "sample", "exec")
namespace = {}
exec(code, namespace)
print(namespace["number"])Top-level scope
'number = 3'Object 2 · code<code object <module>, file "sample", line 1>Object 3 · dict{'number': 3}The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
source = "number = 3"
code = compile(source, "sample", "exec")
namespace = {}
exec(code, namespace)
print(namespace["number"])compile() FunctionThe compile() function in Python is used to compile source code into a code object that can be executed using the exec() or eval() functions.
This is especially useful when you want to execute Python code that is available as a string.
compile(source, filename, mode)
source: A string or AST object containing valid Python code.filename: A name string (usually '<string>' if reading from a string).mode: One of 'exec', 'eval', or 'single'.exec() or eval().'exec': For statements (like loops, function definitions).'eval': For expressions (like 2 + 2).'single': For a single interactive statement (like in the REPL).code = "3 + 5"
compiled_code = compile(code, "", "eval")
result = eval(compiled_code)
print(result)
8
code = """
for i in range(3):
print("Line", i)
"""
compiled = compile(code, "", "exec")
exec(compiled)
Line 0
Line 1
Line 2
code = "print('Hello')"
compiled = compile(code, "", "single")
exec(compiled)
Hello
'eval' for code that includes statements like if or for will raise a SyntaxError.In advanced interviews or coding tools, compile() is used to simulate a mini Python interpreter. It shows your understanding of dynamic code execution.
compile() turns code strings into code objects.eval() or exec() to run the compiled code.'exec' (statements), 'eval' (expressions), 'single' (REPL).Write a program that reads an arithmetic expression from the user, compiles it, and evaluates the result.
user_input = input("Enter an arithmetic expression: ")
code_obj = compile(user_input, "", "eval")
result = eval(code_obj)
print("Result:", result)
Connect what you noticed in the example with the idea below.
compile() turns source text into a code object. Execution is a separate step; the mode must match the kind of source.
compile() always immediately executes the source code.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.