The python error TypeError: object of type ‘type’ has no len() occurs while attempting to find the length of an object that returns ‘type’. The python variables can store object data types. If a python variable refers a data type, it can’t be used in length function. The Length function is used for data structures that store multiple objects. The data type of the variable that store another data type is called ‘type’.
For example list is a data type, list() returns a object of type list. int is a data type, where as int() returns int value. Python supports to store a data type and value in a variable.
a = int -- stores data type
a = int() -- stores a int value
The python variables can store the data types and classes. These variables are not assigned any value, or objects. The length function can not be called the variables which are not allocated with any value or object. If you call the length function for these variables of ‘type’, it will throw the error TypeError: object of type ‘type’ has no len()
The basic data types are int, float, long, bool, complex etc. Some of the collections types are list, tuple, set, dict etc. Python allows to store these data types in a variable. The data type of the variable that store another data type is called ‘type’.
Exception
The error TypeError: object of type ‘type’ has no len() will show the stack trace as below
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'type' has no len()
[Finished in 0.1s with exit code 1]
How to reproduce this issue
If a python variable is assigned with a data type, the type of the variable would have ‘type’. If the length function is invoked for the ‘type’ variable, the error TypeError: object of type ‘type’ has no len() will be thrown.
s=list
print len(s)
Output
Traceback (most recent call last):
File "/Users/python/Desktop/test.py", line 2, in <module>
print len(s)
TypeError: object of type 'type' has no len()
[Finished in 0.1s with exit code 1]
Root Cause
The python variables are used to store any object or value. The values of the primitive data type such as int, float, long, boot etc can be stored in any python variable. Python is an object oriented programming language. The objects can be stored in Python variables. As the python variables are not declared with any data types, python can support to store a python function and a data type.
If a data type (not a value or object) is stored with a python variable, the length function can not be used for such variable. So, the error TypeError: object of type ‘type’ has no len() is thrown
Solution 1
The python variable which is assigned with a data type, should be assigned with a value or object. If the variable is not assigned with any value or object , assign with an object such as list, tuple, set, dictionary etc.
list – is a data type, where as list() is an object of type list
s=list()
print len(s)
Output
0
[Finished in 0.1s]
Solution 2
Due to the dynamic creation of the variable the python variable may not assigned with data types. The datatype of the variable is ‘type’. In this case, the data type must be checked before the length function is called.
s=list
print type(s)
if s is list :
print "Value is type"
else:
print (len(s))
Output
<type 'type'>
Value is type
[Finished in 0.0s]
Solution 3
The python variable should be validated for the data type. The length function is used to find the number of items in the objects. Before calling the length function, the object must be validated for the collection of items.
s=list
print type(s)
if type(s) in (list,tuple,dict, str):
print (len(s))
else:
print "not a list"
Output
<type 'type'>
not a list
[Finished in 0.1s]
Solution 4
The try and except block is used to capture the unusual run time errors. If the python variable contains expected value, then it will execute in the try block. If the python variable contains the unexpected value, then the except block will handle the error.
s=type
print s
try :
print (len(s))
except :
print "Not a list"
Output
<type 'type'>
Not a list
[Finished in 0.0s]