GCGrowCarrer

Dataclasses and Type Hints

Locked

Lesson 23 of 60 · OOP · Medium

Finish these first:Classes and Objects →Or pass the tests below to unlock it now.

1

The big idea

Type hints document intent and enable static checking (mypy/pyright). @dataclass generates init, repr, eq.

Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Key words (tap to learn):
2

Read the lesson

Type hints document intent and enable static checking (mypy/pyright). @dataclass generates __init__, __repr__, __eq__.

from dataclasses import dataclass
@dataclass(frozen=True)
class Point:
    x: float
    y: float = 0.0
def dist(a: Point, b: Point) -> float: ...
3

Quick check

Before writing code: What should `manhattan(Point(0,0),Point(3,4))` return when your implementation of Dataclasses and Type Hints is working?

4

Your challenge

3 sample tests, 1 hidden

Define frozen dataclass Point(x, y) and manhattan(a, b).

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

Similar to java.enums-records (70% overlap). `@dataclass(frozen=True)` ~ Java `record`.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Dataclasses and Type Hints.

Mastery0%

Learn this first

Opens up next

This is the last lesson in this branch.