This error occurs when you try to pass a built-in function or method object to a len()
method call.
If the built-in function or method returns an iterable object like a list or a tuple, you can use the function or method call as the argument for the len()
method by putting parentheses after the function or method name. For example,
data = input("Enter a string: ") print(len(data))
This tutorial will go through how to solve this error with code examples.
Table of contents
TypeError: object of type ‘builtin_function_or_method’ has no len()
We raise a Python TypeError when attempting to perform an illegal operation for a specific data type. Retrieving the length of an object is only suitable for iterable objects, like a list or a tuple. A builtin_function_or_method
is a method or a function that is built into the Python interpreter.
Functions are not iterable objects, therefore if we try to pass a method to the len()
method call, we will raise the TypeError: object of type ‘method’ has no len()
.
The len()
method implicitly calls the dunder method __len__()
which returns a positive integer representing the length of the object on which it is called. All iterable objects have __len__
as an attribute. Let’s check if __len__
is in the list of attributes for the builtin_function_or_method
object and the list object using the built-in dir()
method.
inp = input print(type(inp)) print('__len__' in dir(inp))
<class 'builtin_function_or_method'> False
We can see that __len__ is not present in the attributes of the builtin_function_or_method
object.
lst = [2, 4, 6, 8] print(type(lst)) print('__len__' in dir(lst))
<class 'list'> True
We can see that __len__
is present in the attributes of the list
object.
Example
Let’s look at an example of trying to get the length of a builtin_function_or_method
object. First, we will assign the input
function to the variable name data.
data = input
The input()
function takes user input and by default returns the user input as a string.
Next, we will try to get the length of the data
variable.
print(len(data))
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [12], in <cell line: 2>() 1 data = input ----> 2 print(len(data)) TypeError: object of type 'builtin_function_or_method' has no len()
The error occurs because we did not call the input function. Therefore, Python interprets the len()
call as trying to get the length of the input method, which is not an iterable object with a length.
Solution
We can solve this error by calling the input function and then inputting some text to store as a string.
Python strings are iterable and have a length.
We can call a function by putting parentheses ()
after the function name.
Let’s look at the revised code:
data = input("Enter a string: ") print(len(data))
Let’s run the code to see the result:
Enter a string: Python is fun! 14
We successfully called the input()
function, provided the input “Python is fun!
“, which is stored as a string. Then we calculated the length of the string using the len()
method, which is 14
.
Summary
Congratulations on reading to the end of this tutorial!
For further reading on the has no len()
TypeErrors, go to the article:
- How to Solve Python TypeError: object of type ‘NoneType’ has no len()
- How to Solve Python TypeError: object of type ‘function’ has no len()
To learn more about Python for data science and machine learning, go to the online courses page on Python, which provides the best, easy-to-use online courses.
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.