CodeAlchemy

Jotting one man's journey through software development, programming, and technology


Project maintained by pablogarciaprado Hosted on GitHub Pages — Theme by mattgraham

dataclass

◀️ Home

A dataclass is a class decorator (@dataclass) that automatically generates common methods like __init__, __repr__, and __eq__ based on class fields.

Why use it

Use dataclass when your class is primarily structured data with minimal custom behavior.

Basic example

from dataclasses import dataclass

@dataclass
class User:
    name: str
    age: int = 0

user = User("Pablo", 24)
print(user)  # User(name='Pablo', age=24)

Generated methods explained

Useful options