I'm creating a list-like object that holds a very large number of elements, however when I run the below code, I get an error.
class test:
def __init__(self, n):
self.n = n
def __len__(self):
return self.n
instance = test(10**20)
print(len(instance))
The error that I get back is: OverflowError: cannot fit 'int' into an index-sized integer
Anyone know what's going wrong?
This is a CPython implementation detail. From the __len__
docs:
In CPython, the length is required to be at most
sys.maxsize
. If the length is larger thansys.maxsize
some features (such aslen()
) may raiseOverflowError
. To prevent raising OverflowError by truth value testing, an object must define a__bool__()
method.