TypeError: object of type ‘bool’ has no len()

The python error TypeError: object of type ‘bool’ has no len() occurs when trying to find the length of an object that returns boolean. The python length function can not be used in objects of a boolean type. The Length function is used for objects that hold many objects. Therefore, the length function should be used in objects that can store many boolean values

The length function is used in a structured object such as list, tuple, and dictionary to find the length of the string or the number of objects.The length function does not allow data types such as int, float, long, boolean, and complex data types. If the length function is invoked on unsupported objects then it will throw the error TypeError: object of type ‘bool’ has no len().



Exception

The exception stack trace of the error will be shown as shown below.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    print (len(s))
TypeError: object of type 'bool' has no len()
[Finished in 0.0s with exit code 1]


How to reproduce this error

Create a python variable assigned with a boolean value. If the length function is invoked for the boolean variable, the error TypeError: object of type ‘bool’ has no len() will be thrown.

s= True
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 'bool' has no len()
[Finished in 0.0s 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, bool 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 boolean value.

If a boolean data is stored with a python variable, the length function can not be used for such variable. So, the error TypeError: object of type ‘bool’ has no len() is thrown



Solution 1

The python variable assigned to the boolean value should not be used in the length function. The python variable should be converted to objects such as list, tuple, set, dictionary, etc. In those objects, the boolean value should be added

s= [True ]
print (len(s))

Output

1
[Finished in 0.1s]


Solution 2

If the intended data type of the variable is known as boolean, the data type should be checked before the length function is invoked. If the data type is boolean, take an alternate flow. Otherwise, the length method is called with the python variable.

s= True
print type(s)
if isinstance(s, bool) : 
	print "Value is boolean"
else:
	print (len(s))

Output

<type 'bool'>
Value is boolean
[Finished in 0.1s]


Solution 3

If the intended data type of the variable is unknown, the expected data type should be checked before the length function is invoked. If the data type is a list of objects, the length method is called with the python variable. Otherwise, take an alternate flow.

s= True
print type(s)
if type(s) in (list,tuple,dict, str): 
	print (len(s))
else:
	print "not a list"

Output

<type 'bool'>
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= True
print type(s)
try : 
	print (len(s))
except :
	print "not a list"

Output

<type 'bool'>
not a list
[Finished in 0.1s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *