Skip to content

Conversation

@zyros-dev
Copy link
Owner

@zyros-dev zyros-dev commented Dec 25, 2025

Claude Teach-in

I wanted to learn about CPython internals so then I had Claude step me through some essential parts of the repo, then I completed a project to finish implementing a record type.

cpython ➤ ./python.exe                       git:teaching-cpython-students-work*
Python 3.10.19+ (heads/teaching-cpython-students-work-dirty:005ed8a2c2c, Dec 25 2025, 13:44:15) [Clang 20.1.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Cmd click to launch VS Code Native REPL
>>> f = {|a='asdf', b='zxcv'|}
>>> f
record(a='asdf', b='zxcv')
>>> f.a
'asdf'
>>> 

- CLAUDE.md: Teaching mode instructions and architecture overview
- teaching-todo.md: 5-phase curriculum building toward Record type
- teaching-notes.md: Detailed implementation notes (for Claude reference)

The learning project implements a Record type (immutable named container)
and BUILD_RECORD opcode, covering PyObject fundamentals, type slots,
the evaluation loop, and build system integration.
Add a new immutable Record type with named fields (similar to namedtuple but
implemented entirely in C). This serves as a teaching reference implementation
demonstrating core CPython concepts:

- PyVarObject with variable-length data (r_values array)
- Type slots: tp_repr, tp_hash, tp_richcompare, tp_dealloc, tp_traverse
- Sequence protocol: sq_length, sq_item
- Attribute access: tp_getattro for field name lookup
- GC integration with _PyObject_GC_TRACK and Py_TRASHCAN_*
- Bytecode evaluation loop (BUILD_RECORD opcode)

Features:
- Attribute access: r.field_name
- Indexing: r[0], r[1], r[-1], len(r)
- Hashing: hash(r) - usable as dict key
- Equality: r1 == r2 (compares both names and values)
- Python constructor: Record(x=10, y=20)

Files added:
- Include/recordobject.h: Header with RecordObject struct and API
- Objects/recordobject.c: Full implementation (~490 lines)

Files modified:
- Lib/opcode.py: Added BUILD_RECORD opcode (166)
- Include/opcode.h: Regenerated with BUILD_RECORD
- Python/opcode_targets.h: Regenerated opcode jump table
- Python/ceval.c: Added BUILD_RECORD handler in eval loop
- Makefile.pre.in: Added recordobject.o and header
- Objects/object.c: Added PyRecord_Type initialization
- Python/bltinmodule.c: Exposed Record as builtin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants