How do I make len() work with my class?



In this exercise the len() function is called on a list, a dictionary, and a string. How would I make len() work on a new class?


The len() function will attempt to call a method named __len__() on your class. This is one of the special methods known as a “dunder method” (for double underscore) which Python will look for to perform special operations. Implementing that method will allow the len() call to work on the class. If it is not implemented, the call to len() will fail with an AttributeError. The following code example shows a class implementing __len__().

class Bag:
    def __init__(self, item_count):
        self.item_count = item_count

    def __len__(self):
        return self.item_count

mybag = Bag(10)