Compiled vs Interpreted Languages (in Python terms)
Beginner Satellite
Compiled vs Interpreted
Python blurs the line: it compiles source to bytecode then interprets it. Understanding this hybrid model explains start-up time, `.pyc` files, and optimization options.
Quick definitions
- Compiled language: Source is converted to machine code ahead of time (C, Go).
- Interpreted language: Source is executed line-by-line by an interpreter (classic scripting languages).
- Python: Compiles to bytecode, then a virtual machine interprets that bytecode at runtime.
Bytecode
import dis
def greet():
return "hi"
dis.dis(greet)
The dis module shows the bytecode instructions the interpreter will execute.
Performance implications
- Compilation happens automatically;
.pycfiles cache bytecode for faster startup. - Heavy computation benefits from C extensions, Cython, or PyPy (JIT).
- For most apps, readability and libraries matter more than raw speed.
When to care
- Shipping to resource-constrained environments.
- Interfacing with compiled languages (C, Rust) for performance-critical sections.
- Educating stakeholders on why Python's agility trades some performance.