Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Performance and Profiling

dis and CPython bytecode

Performance and Profiling

Introduction

Python is an INTERPRETED language, but it does not execute source code line by line. It first compiles it into BYTECODE — a sequence of instructions for the CPython virtual machine. Only that bytecode is executed by the evaluation loop (_PyEval_EvalFrameDefault in ceval.c).

The stdlib dis module lets you inspect that bytecode. Type dis.dis(func) and you see instructions: LOAD_FAST, BINARY_ADD, CALL_FUNCTION, RETURN_VALUE. It is a window into what Python actually does — it often explains micro-optimisations (why locals beat globals, why list-comps beat for-loops, why += on strings can be slow).

This lesson covers: reading dis.dis output, the most important opcodes (LOAD_FAST/LOAD_GLOBAL, BUILD_LIST, JUMP_*, CALL), compilation to .pyc in __pycache__, peephole optimisations, the 3.10 → 3.11 differences (PEP 659 specialisation, adaptive interpreter, "inline caches") and using dis to validate optimisations.