Python
# Program to demonstrate some magic methods
class BookCollection:
def __init__(self, books):
self.books = books
def __len__(self):
return len(self.books)
def __str__(self):
return ", ".join(self.books)
bc = BookCollection(["Python 101", "OOP in Python"])
print(len(bc))
print(bc)Output
2 Python 101, OOP in Python
Implementing len and str customizes how len() and print() behave for the class.