[PYTHON] Watch out for the return value of __len__

When I overridden __len__ in my class and called it, the following error occurred.

OverflowError: cannot fit 'int' into an index-sized integer

The cause was in the __len__ specification. Apparently, the return value of __len__ must meet the following conditions.

--Integer type (int) --0 or more --Below and below the upper limit of signed 32-bit integers (0x7FFFFFFF) --The "signed 32-bit integer" part may differ depending on the environment.


class ReturnStr(object):
    def __len__(self): return "abc"

class ReturnFloat(object):
    def __len__(self): return 1.0

class ReturnNegativeNumber(object):
    def __len__(self): return -1

class ReturnBigNumber(object):
    def __len__(self): return 0x7FFFFFFF + 1

class ReturnSmallNumber(object):
    def __len__(self): return 0x7FFFFFFF

if __name__ == "__main__":
    len(ReturnStr())            #=> TypeError: 'str' object cannot be interpreted as an integer
    len(ReturnFloat())          #=> TypeError: 'float' object cannot be interpreted as an integer
    len(ReturnNegativeNumber()) #=> ValueError: __len__() should return >= 0
    len(ReturnBigNumber())      #=> OverflowError: cannot fit 'int' into an index-sized integer
    len(ReturnSmallNumber())    #=> OK

In my case, it seems that it was an exception because the return value of __len__ was too large (´ ・ ω ・`)

Recommended Posts

Watch out for the return value of __len__
About the return value of pthread_mutex_init ()
About the return value of the histogram.
Watch out for randint
Watch out for global variables outside of lambda_handler (datetime)
The story that the return value of tape.gradient () was None
The return value of len or unichr may change depending on whether it is UCS-2 or UCS-4.
Find the definition of the value of errno
Check the return value using PEP 380
2015-03-25 python> Multi-line comment> Watch out for indent
The third night of the loop with for
Pandas of the beginner, by the beginner, for the beginner [Python]
Get the value of the middle layer of NN
The second night of the loop with for
Get the return value of an external shell script (ls) with python3
Make the default value of the argument immutable
[Python] Summary of functions that return the index that takes the closest value in the array
The story of low learning costs for Python
The value of pyTorch torch.var () is not distributed
Try singular value decomposition of the daimyo matrix
Find the divisor of the value entered in python
Image processing? The story of starting Python for
Find out the day of the week with datetime
Code for checking the operation of Python Matplotlib
Search by the value of the instance in the list
Return value of quit ()-Is there anything returned by the "function that ends everything"?
See here for the amount of free memory of the free command
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
Add the attribute of the object of the class with the for statement
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Around the place where the value of Errbot is stored
I tried the MNIST tutorial for beginners of tensorflow.
Return one-hot encoded features to the original category value
[Python] Calculate the average value of the pixel value RGB of the object
Find out the version of the language you are running
Summarized the types of sum of squares for analysis of variance
Get the return code of the Python script from bat
[C language] [Linux] Get the value of environment variable
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Make the default value of the argument immutable (article explanation)
I checked out the versions of Blender and Python
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Find out the location of packages installed with pip
[Golang] Specify an array in the value of map
Techniques for understanding the basis of deep learning decisions
Make up for the lack of Maya Node reference
Check for the existence of BigQuery tables in Java
Date the name of the decomposed PDF for each page
[Django 2.2] Sort and get the value of the relation destination
Things to watch out for when migrating with Django
[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)
[JVM] OOM (Out Of Memory) Necessary knowledge for troubleshooting
[For beginners] Quantify the similarity of sentences with TF-IDF
I searched for the contents of CloudWatch Logs Agent
[Linux] [C / C ++] How to get the return address value of a function and the function name of the caller