Get startedGet started for free

Building a Playlist

Now that you've gotten the hang of building custom iterators, you'll practice creating your own Playlist class, to play your favorite songs.

This exercise is part of the course

Intermediate Object-Oriented Programming in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class Playlist:
  def __init__(self, songs, shuffle=False):
    self.songs = songs
    self.index = 0
    
    if shuffle:
      random.shuffle(self.songs)
  
  # Define a magic method that returns the iterator object
  ____ ____(____):
    return ____
Edit and Run Code