__len__
- VC Healy
- May 27, 2020
- 1 min read
object.__len__(self)
Called to implement the built-in function len().
Should return the length of the object, an integer >= 0.
Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.
CPython implementation detail: In CPython, the length is required to be at most sys.maxsize. If the length is larger than sys.maxsize some features (such as len()) may raise OverflowError. To prevent raising OverflowError by truth value testing, an object must define a __bool__() method.
__len__(self)
Returns the length of the container. Part of the protocol for both immutable and mutable containers.
len(s)
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
Comments