CoursePython from Scratch to Intermediate · Module 9: Classes and Objects · part 63 of 75
Part 63 · Module 9: Classes and Objects

Lesson 4: Type hints and dataclasses, saying what each value is

18 min read·14 Oct 2026
Module 9 · Lesson 4 · About 50 minutes · Needs: Module 9 Lessons 1 to 3 (classes, __init__, __repr__, __eq__), Module 6 Lesson 2 (the mutable default trap), Module 7 Lesson 4 (%pip)

The situation

Tom is reading your code on the café laptop. "This late_fee takes days_late. Is that a number? Could it be the text from the spreadsheet? And every class starts with an __init__ that only copies values onto self."

Both are fair. This lesson fixes them: type hints say what each value is, and dataclasses write the repetitive parts of a class for you. Jonah's loan L04, Frankenstein, 16 days late and 300 pence owed today, runs through the whole lesson.

days_late: int a type hint mypy checks it Python ignores it @dataclass Loan five hinted fields Python writes __init__, __repr__, __eq__
The lesson in one picture: hints are notes that mypy checks and Python ignores (grey), and @dataclass turns hinted fields into a whole class (blue is your code, green is what tools do for you).

The rest of this part opens with an account

Signing up is free and takes one email. Nothing to pay, and nothing renews.