1
def __len__(self):
        """
        Return number of elements sorted in array
        """
        return self.n

To my understanding, we are redefining what the method len will return for our class. Am I correct or there is a bigger picture?

  • `len` It is not a method of your class. What you are doing is implementing a protocol. The function `len` knows (or expects) that the object it takes as a parameter will have that `__len__` method implemented so it will call it. You can think of it as how interfaces work in other languages. – Netwave Oct 01 '20 at 12:12

0 Answers0