A Python Interpreter Written in Python
by xk3 on 4/13/2026, 5:25:50 PM
https://aosabook.org/en/500L/a-python-interpreter-written-in-python.html
Comments
by: BoppreH
> Byterun is a Python interpreter written in Python. This may strike you as odd, but it's no more odd than writing a C compiler in C.<p>I'm not so sure. The difference between a self-hosted compiler and a circular interpreter is that the compiler has a binary artifact that you can store.<p>With an interpreter, you still need <i>some</i> binary to run your interpreter, which will probably be CPython, making the new interpreter redundant. And if you add a language feature to the custom interpreter, and you want to use that feature in the interpreter itself, you need to run the whole chain at runtime: CPython -> Old Interpreter That Understand New Feature -> New Interpreter That Uses New Feature -> Target Program. And the chain only gets longer, each iteration exponentially slower.<p>Meanwhile with self-hosted a compiler, each iteration is "cached" in the form a compiled binary. The chain is only in the history of the binary, not part of the runtime.
4/17/2026, 9:34:15 AM
by: anitil
Oooh it's a bytecode interpreter! I was wondering how they'd fit a parser/tokenizer in 500 lines unless the first was `import tokenizer, parser`. And it looks like 1500ish lines according to tokei<p>I think because python is a stack-based interpreter this is a really great way to get some exposure to how it works if you're not too familiar with C. A nice project!
4/17/2026, 5:56:27 AM
by: vachanmn123
Very well written! Everyone used to tell me during Uni that stacks are used for running programs, never ACTUALLY understood where or how.
4/17/2026, 9:29:24 AM
by: blueybingo
the article glosses over something worth pausing on: the `getattr` trick for dispatching instructions (replacing the big if-elif chain) is actaully a really elegant pattern that shows up in a lot of real interpreters and command dispatchers, not just toy ones -- worth studying that bit specifically if you're building anything with extensible command sets.
4/17/2026, 8:36:16 AM
by: tekknolagi
See also <a href="https://github.com/nedbat/byterun" rel="nofollow">https://github.com/nedbat/byterun</a> and <a href="https://github.com/rocky/x-python" rel="nofollow">https://github.com/rocky/x-python</a>
4/17/2026, 5:48:43 AM
by: woadwarrior01
aka A Metacircular Interpreter
4/17/2026, 7:35:54 AM
by: andltsemi3
"Yaw dog I heard you liked python, so I put python in your python so you can interpret python while you interpret python"
4/17/2026, 8:42:10 AM
by: kevinten10
[dead]
4/17/2026, 8:20:41 AM
by: hcfman
Just wondering why you stopped there? Why not a python interpreter for a python interpreter for python ?
4/17/2026, 6:42:08 AM