L5·Intermediate/Advanced
🏷️Typing & Dataclasses
Static type hints and boilerplate-free classes.
typingdataclasshints
§ 1Hints
def f(x: int) -> str: — not enforced at runtime. Tools like mypy/pyright check them.
§ 2Modern generics (3.9+)
list[int], dict[str, int]. Union: X | Y. Optional: X | None.
§ 3@dataclass
Auto-generates __init__, __repr__, __eq__ from field annotations.
Worked example
1from dataclasses import dataclass23@dataclass4class Point:5 x: float6 y: float7 def dist(self) -> float:8 return (self.x**2 + self.y**2) ** 0.5910print(Point(3, 4))11print(Point(3, 4).dist())Try it live · Python runs in your browser
Loading playground…
Checkpoint quiz
Question 1 / 2
Are Python type hints enforced at runtime?
Lesson test
Take an 8-question test on “Typing & Dataclasses” — concepts, output prediction, bug spotting and interview questions.