Python AST Bytecode & Type Disparity Explorer

CPython 3.12 Architecture

Analyze how Python evaluates expressions such as 2 + 2 versus "2" + "2": from lexing & Abstract Syntax Tree nodes to compiler stack bytecode and C-level type dispatch (long_add vs unicode_concat).

Test Presets:
>>>

Bytecode Instructions

5 Opcodes

CPython instruction pointer stream evaluated by _PyEval_EvalFrameDefault:

Instructions follow Python 3.12+ RESUME and unified BINARY_OP format.

CPython AST Node Tree

_ast.BinOp
Green = String Literal Blue = Int/Float Literal Teal = Expression Root

CPython Value Stack

Depth: 0
Frame stack empty (press Step or Run)
C Type Dispatch Mechanism
Awaiting VM execution step...
Evaluated Output (Value) "22"
CPython Return Type str
Internal C Dispatch unicode_concat()
Execution Phase Completed (RETURN_VALUE)
Observed VM Action
Calls __add__ on string object, returning concatenated result '22'

Why doesn't "2" + "2" equal 4?

Python does not perform implicit type coercion during binary operations. In CPython, every object possesses an ob_type pointer. When executing the BINARY_OP (+) opcode, the virtual machine extracts the operands and checks their type slots: for strings, it calls PyUnicode_Concat() which allocates a new unicode buffer combining '2' and '2' into '22'. For integers, it invokes long_add() which extracts C digit arrays to calculate 2 + 2 = 4. If you mix "2" + 2, neither type provides a compatible conversion slot, raising an immediate TypeError.

Super generates helpful tools and automates fact-checking across the internet proactively. If you enjoyed this tool, build your own with Super and share it with a friend.