This error occurs when you try to pass a function to a len()
method call. If the function returns an iterable object like a list or a tuple, you can use the function call as the argument for the len()
method by putting parentheses after the function name. For example,
def get_list(): lst = ['x', 'y', 'z'] return lst print(len(get_list()))
This tutorial will go through the error and how to solve it with code examples.
TypeError: object of type ‘function’ 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 function is a block of code which only runs when it is called.
We can verify that an object is callable using the built-in callable()
method. If the callable()
method call returns True, the object is callable.
def list_func(): return [1, 2, 3, 4] print(callable(list_func))
True
Functions are not iterable objects, therefore if we try to pass a function to the len()
method call, we will raise the TypeError: object of type ‘function’ has no len()
.
Example
Let’s look at an example of trying to get the length of a function. First, we will define a function that returns a tuple of integers when called.
def get_tuple(): tup = (2, 4, 6, 8) return tup
Next, we will try to get the length of the tuple returned by the get_tuple
function using the len()
method.
print(len(get_tuple))
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [2], in <cell line: 1>() ----> 1 print(len(get_tuple)) TypeError: object of type 'function' has no len()
The error occurs because we did not call the function to return the tuple. Therefore, Python interprets the len()
method call as trying to get the length of the function, which is not an iterable sequence.
We can check the type of objects using the built-in type()
function. Let’s check the type of get_tuple
and the object returned by the get_tuple()
call.
print(type(get_tuple)) print(type(get_tuple())
<class 'function'> <class 'tuple'>
We can see that if we do not put parentheses after the function name, the object is a function. If we call the function we get the object the function returns, which in this case is a tuple.
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 function object and the tuple object using the built-in dir()
method.
print('__len__' in dir(get_tuple)) print('__len__' in dir(get_tuple()))
False True
We can see that __len__
is an attribute of the tuple object, which means we can use it as the argument for the len()
method.
Solution
We can solve this error by calling the get_tuple
function. We can call a function by putting parentheses ()
after the function name. Let’s look at the revised code:
def get_tuple(): tup = (2, 4, 6, 8) return tup print(len(get_tuple()))
Let’s run the code to see the result:
4
We successfully called the get_tuple()
function to return the tuple, and used the len()
method to get the length of the tuple.
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 ‘method’ 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.