Dataclass Inheritance
Advanced · Write the code · from Advanced Dataclasses: __post_init__, frozen, field(), slots, and Inheritance
From the lesson: Advanced Dataclasses: __post_init__, frozen, field(), slots, and Inheritance
You already know the basics of dataclasses - how to define fields, get automatic __init__ and __repr__, and set default values. Now it's time to unlock the advanced features.
Read the lessonCreate a parent dataclass Employee with fields name (str) and department (str, default 'General'). Create a child dataclass Manager that inherits from Employee and adds a field team_size (int, default 5). Create a Manager('Alice') and print it.
from dataclasses import dataclass
# Create Employee dataclass
# Create Manager dataclass inheriting from Employee
# Create a manager and print
More Advanced Dataclass exercises
- Validate with __post_init__Beginner
- Safe List Default with field()Beginner
- Predict the Output: Frozen DataclassIntermediate
- Computed Field with __post_init__Intermediate
- Refactor: Add Validation and Computed FieldsAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz