class Library clas Book lib = Library(1, '51 Some str., NY') lib += Book('Leo Tolstoi', 'War and Peace') lib += Book('Charles Dickens', 'David Copperfield') for book in lib: # вывод в виде: [1] L.Tolstoi 'War and Peace' print(book) # вывод в виде: ['War', 'Peace'] print(book.tag()) |
1 answer
Override the __iadd__ magic method to support the += operator class and the __iter__ method to support iteration:
class Library: def __init__(self, id, address): self.id = id self.address = address self._books = [] def __iadd__(self, book): self._books.append(book) return self def __iter__(self): for book in self._books: yield book - It is not clear how I can connect classes - Sasha
- What do you mean by "tie"? - Sergey Gornostaev
|