🌱 Junior Track 24 of 24
24. Dataclasses
Eliminate class boilerplate using `@dataclass`, configure default values & `default_factory`, create immutable hashable objects with `frozen=True`, fine-tune attributes with `field()`, and leverage `__post_init__`.
👁️ Views: 0
🎓 Junior Python Curriculum — Select Module
1️⃣ Fundamentals
2️⃣ Data Types
3️⃣ Operators
4️⃣ Conditionals
5️⃣ Loops
6️⃣ Collections
7️⃣ Comprehensions
8️⃣ Functions
9️⃣ *args & **kwargs
🔟 Scope & Namespaces
1️⃣1️⃣ OOP
1️⃣2️⃣ Exceptions
1️⃣3️⃣ File Handling
1️⃣4️⃣ Modules & Packages
1️⃣5️⃣ Virtual Environments
1️⃣6️⃣ Standard Library
1️⃣7️⃣ Iterators & Iterables
1️⃣8️⃣ Generators
1️⃣9️⃣ Decorators
2️⃣0️⃣ Context Managers
2️⃣1️⃣ Lambda Functions
2️⃣2️⃣ Functional Programming
2️⃣3️⃣ Type Hints
2️⃣4️⃣ Dataclasses
2️⃣5️⃣ Testing
2️⃣6️⃣ Debugging
2️⃣7️⃣ Logging
2️⃣8️⃣ JSON & Data
2️⃣9️⃣ Regular Expressions
3️⃣0️⃣ Databases
3️⃣1️⃣ APIs & HTTP
🌱 Junior Track 24 Master Reference
24. Dataclasses & Field Configuration Guide
📊 Dataclass vs Normal Class vs NamedTuple
| Feature | @dataclass | Normal Class | NamedTuple |
|---|---|---|---|
| __init__ Generation | Automatic | Manual | Automatic |
| __repr__ Output | Readable representation | Memory address fallback | Readable tuple format |
| Value Equality (__eq__) | By Field Values | Identity (is) by default | By Element Values |
| Mutability | Mutable or Frozen | Mutable by default | Immutable Tuple |
| Custom Methods | Full Support | Full Support | Limited Support |
⚙️ field() Parameters Quick Reference
| Parameter | Type | Purpose & Behavior |
|---|---|---|
| default | Any | Literal default value for primitive types (str, int, float, bool). |
| default_factory | Callable | Zero-argument function (e.g. list, dict) called to create a fresh mutable object for each instance. |
| repr | bool | If False, excludes field from auto-generated __repr__ (great for secrets). |
| compare | bool | If False, ignores field during equality (==) and comparison methods. |
| init | bool | If False, excludes field from __init__ parameters (useful when calculated in __post_init__). |