92.4k views
4 votes
Write the Python code to create a class by performing the following tasks:

1. Define the Music class by adding the firmware_version class attribute.
2. Define the initializer method and pre-populate the tracklist with the Spirit in the sky, Starman, and Escape songs. Make sure the music track's store is private.
3. Define the play method, which sets the current track attribute to the first item in the track's list.
4. Define the list_tracks method, which returns the list of tracks

User HellCatVN
by
8.3k points

1 Answer

3 votes

Final answer:

To create a class in Python that meets the given requirements, define a Music class with a firmware_version class attribute, an initializer method that pre-populates the private tracklist attribute with songs, a play method, and a list_tracks method. The play method sets the current track attribute to the first item in the tracklist, and the list_tracks method returns the tracklist.

Step-by-step explanation:

To create a class in Python that meets the given requirements, you can define a Music class with the firmware_version class attribute. In the initializer method, you can pre-populate the tracklist attribute with the songs 'Spirit in the sky', 'Starman', and 'Escape'. Make sure to make the tracklist attribute private by prefixing it with double underscores. The play method can set the current track attribute to the first item in the tracklist, and the list_tracks method can return the tracklist.

Here's the code:

class Music: firmware_version = '1.0' def __init__(self): self.__tracklist = ['Spirit in the sky', 'Starman', 'Escape'] def play(self): self.current_track = self.__tracklist[0] def list_tracks(self): return self.__tracklist
User Nitheesh
by
8.6k points