Software design and clean code

Stage 1 · Lesson 8 of 8

Pythonic OOP: dataclasses, Protocol and properties

Object-oriented programming properly

Python has no private keyword. A leading underscore, such as _balance, tells other developers a name is internal, and @property lets you expose a read-only value with attribute syntax. write the , equality and printing for classes that mostly hold data, and frozen=True makes them .

For , typing.Protocol describes the methods a type needs, and a type checker such as mypy or Pyright verifies it without any . abc.ABC gives you abstract base classes when you want shared code. Python also has first-class functions, so a one-method class is often better as a plain function.

Common trapwriting Java-style Python, with getters, setters and a class for everything, when a function or a dataclass would be clearer.

A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

Practise

  • Free exercises in your language with optional human mentoring on design, not just on passing tests

Keep handy

  • Read chapter 9, Classes, then the dataclasses and typing pages in the standard library docs

Also worth your time

Mark this stage as not done?