Python etc / `__length_hint__`


PEP 424 allows generators and other iterable objects that don't have the exact predefined size to expose a length hint. For example, the following generator will likely return ~50 elements:

(x for x in range(100) if random() > 0.5)

If you write an iterable and want to add the hint, define the __length_hint__ method. If the length is known for sure, use __len__ instead.

If you use an iterable and want to know its expected length, use operator.length_hint.