Python - How To Fix Python Typeerror: Object of Type Nonetype Has No Len() Error

ID : 502

viewed : 84

Tags : PythonPython ErrorPython NoneType

vote vote

94

This compact guide discusses solving the typeerror: object of type 'nonetype' has no len() error in Python. There’s a simple explanation for this, and to understand everything clearly, let’s learn a bit about NoneType in Python.

Fix typeerror: object of type 'nonetype' has no len() Error in Python

When we assign a none value to any data set in Python, there is no length of it, and that is why you can not use the len() function for this. Take a look at the following code as an example.

#an object of None type i = None #calling function to check the len  len(i)#error 

Finding the length of i of NoneType in the above code will give this error. If you are not familiar with whether your function will work or not, you can always use the built-in methods to find if there is a function for it or not.

For this error, we can find the magic functions of the none object using the following code.

#an object of None type i = None #calling function to check the len  #len(i) #error #built-in / magic functions of None object print(dir(i)) 

Below will be the output for the above code, and as you can see, there is no length function that you can use to find the length of the none object.

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] 

  • Related HOW TO?