Special Method Protocols

__length_hint__() — containers protocol

Supply an estimated length for optional preallocation when exact length is not available. Return a nonnegative integer or NotImplemented. The estimate need not match eventual iteration; it must not control correctness.

Formal Syntax & Structure

python syntaxpython
def __length_hint__(self):

Return Value

int | NotImplementedType

Estimated length or decline.

Working Example

Example Snippetpython
import operatorclass Stream:    def __length_hint__(self): return 12print(operator.length_hint(Stream()))
Python Reference