Editor

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 lesson

Create a dataclass called Playlist with:

  • name (str) - no default
  • songs (list) - default to an empty list using field(default_factory=list)
  • rating (float) - default to 0.0
  • Create 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

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