A Python Interpreter Written in Python

157 points - 04/13/2026

Source

Comments

BoppreH last Friday at 9:34 AM
> 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.

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.

With an interpreter, you still need some 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.

Meanwhile with a self-hosted 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.

---

Edit since this is now a top comment: I'm not complaining about the project! Interpreters are cool, and this is genuinely useful for learning and experimentation. It's also nice to demystify our tools.

anitil last Friday at 5:56 AM
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

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!

cestith last Friday at 7:42 PM
The article contrasts Python to Perl, saying Perl is purely interpreted while Python has compilation. This is factually incorrect.

Perl is transformed into an AST. Then that is decorated into an opcode tree. The thing runs code nearly as fast as C in many instances, once the startup has completed and the code is actually running.

throwpoaster last Friday at 1:22 PM
jgbuddy last Friday at 2:09 PM
one liner:

eval(str)

tekknolagi last Friday at 5:48 AM
vachanmn123 last Friday at 9:29 AM
Very well written! Everyone used to tell me during Uni that stacks are used for running programs, never ACTUALLY understood where or how.
woadwarrior01 last Friday at 7:35 AM
aka A Metacircular Interpreter
blueybingo last Friday at 8:36 AM
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.
gield last Friday at 11:30 AM
(2012)
andltsemi3 last Friday at 8:42 AM
"Yaw dog I heard you liked python, so I put python in your python so you can interpret python while you interpret python"
060880 last Friday at 3:42 PM
[dead]
kevinten10 last Friday at 8:20 AM
[dead]
hcfman last Friday at 6:42 AM
Just wondering why you stopped there? Why not a python interpreter for a python interpreter for python ?