Dataclass

A dataclass is a Python feature that automatically generates common methods (such as __init__, __repr__, and __eq__) for classes that primarily store data. It reduces boilerplate code and makes data models easier to read and maintain.

Overview

from dataclasses import dataclass

@dataclass(slots=True, frozen=True)
class search_element:
    ...

slots=True

Using slots=True creates a fixed set of attributes for each instance instead of a dynamic __dict__.

Benefits:

  • Reduces memory usage.
  • Improves attribute access performance.
  • Prevents adding new attributes at runtime.

frozen=True

Using frozen=True makes instances effectively immutable after creation.

Benefits:

  • Prevents accidental modification of object state.
  • Makes objects safer to share across code.
  • Allows instances to be hashable (when appropriate), so they can be used as dictionary keys or stored in sets.
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9