compile() — Python Technical Interview Questions & Answers
compile()
compile()
- compile() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- compile() — output: 5. compile then eval — advanced, rarely needed in viva.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix compile up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call compile() once and say what prints.
Code Examples for compile()
Example
# compile()
c = compile("print(2+3)", "<s>", "eval")
print(eval(c))
compile() — output: 5. compile then eval — advanced, rarely needed in viva.
How to Explain compile() to an Interviewer
Explain compile() as if you are teaching a junior — definition, then one tiny script.
compile() is built-in — no import. One call + exact output.
What does the example print, and why?
compile() — output: 5
What mistake do freshers make with compile()?
Wrong type → TypeError.