Explain the internal execution architecture of Python. What are Bytecode and the Python Virtual Machine (PVM)?
- Hybrid Model: Python is compiled to intermediate bytecode first, then interpreted by the PVM.
- Compilation Step: CPython compiles
.pyinto platform-independent.pycstored in__pycache__. - PVM Runtime: The PVM is an evaluation loop in C that translates bytecode opcodes into native machine code.
- Caching Mechanism: If the source file timestamp is unmodified, compilation is skipped on subsequent runs.
- Portability Advantage: The same
.pycbytecode executes on Windows, Linux, and macOS without changes.
Paragraph Elaboration Blueprint for Exam:
Introduction: Start by writing that Python is popularly categorized as an interpreted language, but technically it implements a two-stage hybrid architecture combining compilation and interpretation.
Stage 1 (Compilation to Bytecode): When a script (e.g. test.py) is executed, CPython first converts the high-level statements into an Abstract Syntax Tree (AST), performs symbol binding, and produces compact, platform-neutral instructions called Bytecode. These are saved inside the __pycache__ folder with a .pyc extension.
Stage 2 (Interpretation by PVM): The Python Virtual Machine (PVM) is not a virtual hardware emulator; it is a software runtime loop written in C. It continuously fetches bytecode instructions, decodes the opcode, and maps them to native host OS operations.
.py फाइल पहिले भित्रभित्रै .pyc (Bytecode) मा बदलिन्छ, अनि PVM ले त्यसलाई लाइन-बाइ-लाइन पढेर कम्प्युटरको प्रोसेसरलाई बुझाउँछ।