Topics / 🐍 Python Foundations / 🧙 Level 9: Classes and Objects

Python exercise · about 5 minutes · free

dataclasses: Records Without Boilerplate

Most classes in data code are just records. @dataclass writes __init__, __repr__ and __eq__ for you.

A decorator is the @name line written just above a class or function: it hands that class to another piece of code, which adds features to it. Decorate a class with @dataclass and list fields with type hints. For mutable defaults like lists, use field(default_factory=list) so objects don't share one list.

Prices, versions and service names last checked 2026-11-18. They change, so confirm on the official pages before you rely on them.

Plain class: def __init__(self, ...): ..., def __repr__(self): ..., def __eq__(self, other): .... @dataclass: @dataclass, class Item:, name: str, price: int, tags: list = field(, default_factory=list)✗ Plain classdef __init__(self, ...): ...def __repr__(self): ...def __eq__(self, other): ...✓ @dataclass@dataclassclass Item:name: strprice: inttags: list = field(default_factory=list)
Your quest: Define dataclass Item with name: str, price: int and tags: list (default empty list), then create sword = Item("Sword", 150, ["melee"]).
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint@dataclass class Item: name: str price: int tags: list = field(default_factory=list)
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 9: Classes and Objects

Share on LinkedIn