Interview Prep/Python for DevOps/Language essentials & functions

What is a dataclass and why use one in automation code?

IntermediateConceptLanguage essentials & functions
Answer

@dataclass generates __init__, __repr__ and __eq__ from annotated fields. It gives you typed, self-documenting records instead of dicts with magic string keys.

from dataclasses import dataclass, field

@dataclass(frozen=True)
class Finding:
    check: str
    resource_id: str
    severity: str = "LOW"
    tags: dict[str, str] = field(default_factory=dict)

frozen=True makes it immutable and hashable; field(default_factory=...) avoids the mutable default bug; asdict() turns it into JSON-ready data. For validation of untrusted input, pydantic models go further.

All Python for DevOps questions Roadmap: Python for automation

More on Language essentials & functions