Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
Dataclasses & NamedTuples
field(default_factory=...) vs mutable default ValueError@dataclass(frozen=True) immutability & hashing__post_init__ invariant validation
Core Concept
`@dataclass` auto-generates clean `__init__`, readable `__repr__`, and field-wise `__eq__` for data-holding classes without boilerplate. Add `frozen=True` to make instances immutable and hashable (usable in sets/dicts!). For mutable defaults like lists/dicts inside a dataclass, NEVER write `tags: list[str] = []`—Python raises `ValueError`; ALWAYS use `field(default_factory=list)`!
Worked Example
`@dataclass(frozen=True) class SkillScore: slug: str; score: int` gives equality (`a == b`), readable `repr`, AND hashability inside `set`s in 3 lines!
Common Pitfall
putting mutable defaults directly on dataclass fields, or forgetting `frozen=True` when comparing/hashing value objects.
Recommended Readings & Practice
Practice Dataclasses & NamedTuples with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.