I want to use big numbers for length. >>> class A: ... __len__ = lambda self: 10 ** 20 >>> len(A()) Traceback (most recent call last): File "<pyshell#5>", line 1, in len(A()) OverflowError: cannot fit 'int' into an index-sized integer
Whoops; sorry -- accidental title change by typing `__len__` into something that wasn't the search box. Stupid fingers... (I suspect this issue is a duplicate of an existing issue.)
If `len()` signature can't be changed to return Python int objects (unlimited) then the OverflowError may contain the actual `.length` property instead (based on by Antoine Pitrou) operator.length(): def length(sized): """Return the true (possibly large) length of `sized` object. It is equivalent to len(sized) if len doesn't raise OverflowError i.e., if the length is less than sys.maxsize on CPython; otherwise return OverflowError.length attribute """ try: return len(sized) except OverflowError as e: return e.length
I recommend this be closed: too much impact on existing code for too little benefit. CPython has historically imposed some artificial implementation specific details in order make the implementation cleaner and faster internally (i.e. a limit on the number of function arguments, sys.maxsize limits, etc.)