A class that keeps a list
Many classes keep a collection of things in a list attribute and offer methods to add to it, take from it and ask about it. The client doesn't touch the list itself; it uses the methods.
The string method join from part 4 comes in handy for showing such a list. As a reminder, it puts the strings of a list together into one string, with the string it is called on between them:
>>> names = ["Ann", "Bo", "Cy"]
>>> ", ".join(names)
'Ann, Bo, Cy'
>>> " / ".join(names)
'Ann / Bo / Cy'Read ", ".join(names) as "join the names with a comma and a space". All the items have to be strings.
Here is a waiting list for a busy restaurant. The first to arrive is the first to get a table:
class WaitingList:
def __init__(self):
self.names = []
def add(self, name):
self.names.append(name)
def next_up(self):
return self.names.pop(0)
def size(self):
return len(self.names)
def __str__(self):
if len(self.names) == 0:
return "nobody waiting"
return ", ".join(self.names)
tables = WaitingList()
tables.add("Ann")
tables.add("Bo")
tables.add("Cy")
print(tables)
print("Table for", tables.next_up())
print(tables, "/", tables.size(), "waiting")
Ann, Bo, Cy
Table for Ann
Bo, Cy / 2 waitingnext_up both changes the list and returns the name it took out, thanks to pop(0). The constructor makes a new empty list for every waiting list, so two restaurants never share one.
A list of tuples
When each thing in the collection has several pieces of data, the list can hold tuples. This playlist keeps each song as a pair of its title and its length in seconds:
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def add(self, title, seconds):
self.songs.append((title, seconds))
def total_seconds(self):
total = 0
for title, seconds in self.songs:
total += seconds
return total
def longest(self):
best = self.songs[0]
for song in self.songs:
if song[1] > best[1]:
best = song
return best[0]
def __str__(self):
titles = []
for title, seconds in self.songs:
titles.append(title)
return f"{self.name}: " + ", ".join(titles)
road_trip = Playlist("Road trip")
road_trip.add("Highway Song", 215)
road_trip.add("Desert Wind", 342)
road_trip.add("Gas Station Blues", 187)
print(road_trip)
print(road_trip.total_seconds(), "seconds")
print("Longest:", road_trip.longest())
Road trip: Highway Song, Desert Wind, Gas Station Blues
744 seconds
Longest: Desert Windfor title, seconds in self.songs: takes each tuple apart into two variables, as you did with a dictionary's items(). In longest, song[1] is a song's length and best[0] the title of the longest one.