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.

Level: intermediateTopic: Python advanced

__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.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.