AI engineering glossary · Python advanced
What is __len__?
__len__: The special method that returns how many items an object holds, called by the built-in len(). Defining it also makes an object usable in truth tests, where zero length counts as false.
__len__: an example
class Batch: def __len__(self): return len(self.rows) makes len(batch) return the number of rows.
Related terms
More Python advanced terms
Where you learn it
__len__ is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.