Editor

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 lesson

Create 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

All Python exercises · 50 practice problems · Object-Oriented Programming quiz