Mental Model: Tiny Text Editor in Python

If you can write functions, you can build this editor. The trick is keeping one clear mental model while the program grows.

Three-Part Model

Think in three parts:

  1. Editor state: lines, file path, and dirty flag.
  2. Command loop: one command in, one state change out.
  3. File boundary: load at start, save on demand.

That pattern shows up in tons of real tools.

Python Shape of the Model

dataclass gives you a tidy home for mutable state.

  • lines: list[str] is your document.
  • dirty: bool protects against accidental loss.
  • methods (append, edit, delete) are controlled writes.

Python stays readable when state has one obvious owner.

The Main Loop Is a Dispatcher

Your while True loop should mostly dispatch.

  • parse command
  • call the right method
  • print feedback

If business logic leaks into every elif, move it back into the class.

Python-Specific Insight

pathlib.Path and list operations keep this beautifully simple.

  • read_text().splitlines() for load
  • list index math for edit/delete
  • "\n".join(lines) for save

No extra packages, no magic.

Build It in Safe Steps

  1. show + append
  2. edit + delete
  3. save + load
  4. unsaved-quit check

Ship each step before moving on.

Common Beginner Mistakes

  • storing state in globals instead of one object
  • skipping index bounds checks
  • mixing input parsing with data mutation

Keep those cleanly separated and you’ll move faster.

Stretch Goals

Try search, replace, or a one-level undo. If each new feature still fits your three-part model, you’re building with professional instincts.