I am attempting to index the book_list object below to be able to access the values of the attributes "title" and "year" in the Books objects (book1 and book2) :
The code:
class Books:
def __init__(self, title ='', year = 1):
self.title = title
self.year = year
def __getitem__(self):
return self.title
return self.year
class BookList(Books):
def __init__(self):
self.book_list = []
def store_book(self, book):
self.book_list.append(book)
def __getitem__(self):
return self.book_list[index]
def showbook(self):
for book in self.book_list:
print(book[0], book[1])
book1 = Books('Title 1', 1000)
book2 = Books('Title 2', 1211)
book_list = BookList()
book_list.store_book(book1)
book_list.store_book(book2)
book_list.showbook()
The current output:
TypeError: __getitem__() takes 1 positional argument but 2 were given
The desired output:
Title 1 1000
Title 2 1211