Playlist with Default Tags
Intermediate · Write the code · 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 lessonCreate a dataclass called Playlist with:
name (str) - no defaultsongs (list) - default to an empty list using field(default_factory=list)rating (float) - default to 0.0Create a playlist called 'Road Trip', add 'Bohemian Rhapsody' and 'Hotel California' to its songs list, set its rating to 4.5, and print the playlist.
from dataclasses import dataclass, field
# Create the Playlist dataclass
More Dataclass exercises
- Create a Product DataclassBeginner
- Fix the Dataclass BugIntermediate
- 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