Dev Helpers

Python / Classes and typing

Define a dataclass

Dev Helpers · September 27, 2025

A dataclass defines the shape of a small record while generating routine initialization and representation methods.

Worked example

from dataclasses import dataclass
@dataclass
class Point:
    x: int
    y: int
print(Point(3, 4))

Result

Point(x=3, y=4)

Implementation notes

Generated equality compares the declared fields by default. Consider whether operational metadata should participate when using instances as records in tests.

Try it yourself

Add a mutable list field with a default factory and compare two instances.

Open the Python example →

Editorial date September 27, 2025 · Published December 6, 2026

Related articles

Run an async function

An async function call creates a coroutine. A top-level runner supplies the event loop that executes it.

Transpose a matrix

Transposing rows into columns assumes a decision about uneven rows. Ordinary zip stops at the shortest input.

Unpack with a remainder

Starred unpacking separates a fixed header from a variable-length tail. It still requires enough elements for the fixed positions.