01 · Why
Why another Python?
Browser Python normally means shipping a large runtime. Natter is a compact interpreter that implements the part of Python taught in school, and implements it so that the things teachers need are easy.
~38 KBSmall start
The core starts at about 38 KB gzipped. Modules such as
re, numpy, forms or Pygame Zero load only when a program imports them (15 to 40 KB each).while True:Never freezes
Programs run in slices with an instruction budget. An endless loop stops and shows the line it is on.
step("over")Debugger included
Breakpoints, step into, over and out, variables and call stack. No patched runtime needed.
Did you mean…?Friendly errors
Messages in English and German with hints such as “Did you mean…?”, in the style of CPython 3.12.
seed: 42Reproducible
Optional fixed random seed and platform-independent math, so every student sees the same output.
== CPythonReal Python
Code is valid Python. What runs here should run in CPython, and the test suite compares both.
02 · Inside
How it works
- Source
- Lexer
- Parser
- Compiler
- Bytecode VM
- A stack machine with its own frame stack
- It does not use JavaScript recursion, so it can stop and resume after any instruction.
- Budgeted execution
run(task, n)executes at mostninstructions, then hands control back to the page.- Suspend and resume
- A native function such as
input()or an animatedforward(100)can park the program and continue it later. Blocking calls look like normal Python. - Tasks
- The main program, every event handler and every frame of a game loop are separate tasks with their own budget.
- Host bridge
- Modules (turtle, games, forms, plots) talk to the VM through host objects. The core knows nothing about the DOM.
- Debugger
- The compiler marks statement starts. The VM checks breakpoints and step mode there and can report variables and the call stack at any pause.
03 · Language
The language
A subset of Python 3 that behaves like Python and prints the same output. The aim is the part used in schools, documented exactly.
| Area | Contents | Status |
|---|---|---|
| Basics | Integers of any size, floats, strings with f-strings and format, lists, tuples, dicts, slices, if/elif/else, while, for with else, break, continue, pass, tuple swap a, b = b, a | in the base |
| Functions | def with defaults, *args, **kwargs, closures, global, nonlocal, lambda, decorators, unpacking, del, assert | in the base |
| Comprehensions | List and dict comprehensions | in the base |
| Classes | class, __init__, inheritance, super(), __str__, __repr__, @property, operator methods such as __add__ | in the base |
| Exceptions | try/except/else/finally, raise, custom exception classes | in the base |
| Sets, generators | set, frozenset, yield, lazy generator expressions | in the base |
| Input | input() (the page waits for the answer), with and context managers, sys.exit(), print(file=…) | in the base |
| Files | open() with read, write and append, os and os.path, import of your own files. The project lives in memory; the host keeps it where it likes (folder, ZIP, browser storage) | in the base |
| Type hints | Parsed and ignored, so code from books runs | in the base |
Threads, async, eval, exec | Not useful in a classroom and hard to make safe | not planned |
Errors carry the line, a call stack and a hint. Numbers behave like CPython: integers become big integers above 253, round() uses banker's rounding, // and % follow CPython.
04 · Modules
Modules
Each module is a separate package that talks to the core through the host bridge, so embeds only load what they use.
| Module | What it offers | Status |
|---|---|---|
| random | randint, choice, choices, sample, shuffle, uniform, gauss, seed … | in the base |
| stdlib | time (virtual clock, no real waiting), datetime, json, statistics, collections, itertools, functools, string, copy, sys, re, pprint, textwrap, and all of math (sin, cos, exp, log …) | in the base |
| turtle | Draw with a turtle, several turtles, colors, fill, speed. The movement and commands are done and match CPython; drawn in the browser | in the base |
| kara | A small ladybug that learns to move, turn and sense. The rules and commands are done; drawn in the browser | in the base |
| pgzero | The full Pygame Zero reference: draw(), update(), event hooks, Actor, Rect, keyboard, screen, images, sounds, music, clock, animate, tone, text formatting. Runs with or without import pgzrun. The game logic is done and matches real Pygame Zero 1.2.1; drawn in the browser by the web host | in the base |
| forms | Windows with buttons, text boxes and lists using the guizero API (a subset), laid out on a grid that grows with its content. The widgets, layout and events are done and match real guizero; drawn in the browser by the web host | in the base |
| numpy, plots | A subset of numpy arrays (printing, indexing, statistics and random numbers match numpy) and matplotlib charts drawn as SVG. Done and compared with the real libraries; the page shows the figure through the browser host | in the base |
| requests | requests.get(url).json() through the browser, for servers that allow it (CORS). The calls and the CORS explanation are done; the page does the actual fetch | in the base |
| Server code, scraping, ML | bottle, sqlite3, BeautifulSoup, keras | not planned |
The command reference lists every module, function, class and constant with its parameters, notes on deviations from CPython and a runnable example. It is generated from the interpreter's source, so it cannot drift from what Natter really does.
05 · Determinism
Same program, same output
Same math everywhere
Addition, multiplication and sqrt are exact IEEE 754. Functions that browsers round differently, like sin and exp, are implemented inside Natter so results are identical on every device.
Random seed
The seed is an optional parameter. Without it, the seed comes from the browser's secure randomness and every run differs, as in normal Python. With it, every run is the same. The seed that was used can be shown to reproduce a run.
Limits
Recursion depth 1000 like CPython, fixed limits for list and string size, independent of the browser's JavaScript stack.