dataclassesEasy Examples

Decorator for auto-generating class boilerplate (__init__, __repr__, etc.)

Getting started with dataclasses

Basic import and usage of the dataclasses module.

python
from dataclasses import dataclass, field

@dataclass
class Point:
    x: float
    y: float

@dataclass
class Person:
    name: str
    age: int
    hobbies: list = field(default_factory=list)

p = Point(3.0, 4.0)
print(p)

person = Person("Alice", 30, ["reading", "coding"])
print(person)

The dataclasses module is part of Python's standard library. Decorator for auto-generating class boilerplate (__init__, __repr__, etc.).

Common dataclasses operations

Frequently used functions from the dataclasses module.

python
# More dataclasses examples
import dataclasses

print(f"dataclasses module loaded successfully")
print(f"Location: {dataclasses.__name__}")
print(f"Has {len(dir(dataclasses))} attributes")

These are the most commonly used features of the dataclasses module.

Want to try these examples interactively?

Open Easy Playground