Fix the Dataclass Bug
Intermediate · Fix the bug · from Python Dataclasses: Less Boilerplate, More Readable Code
From the lesson: Python Dataclasses: Less Boilerplate, More Readable Code
Imagine filling out a form where you have to write your name, address, and phone number. Now imagine filling out 50 forms where every single one asks for the same information in a slightly different format. That's what writing Python classes feels like sometimes.
Read the lessonThis dataclass has a bug - fields with defaults come before fields without defaults. Fix the field order so the output is:
Student(name='Alice', grade=10, gpa=3.9)from dataclasses import dataclass
@dataclass
class Student:
grade: int = 10
name: str
gpa: float
s = Student('Alice', 10, 3.9)
print(s)
More Dataclass exercises
- Create a Product DataclassBeginner
- Playlist with Default TagsIntermediate
- Rectangle with Computed AreaIntermediate
- Predict Frozen BehaviorIntermediate
- Sort Students by GPAIntermediate
- Refactor to a DataclassAdvanced
All Python exercises · 50 practice problems · Object-Oriented Programming quiz