How to Solve Python TypeError: object of type ‘method’ has no len()

by | Programming, Python, Tips

This error occurs when you try to pass a method to a len() method call. If the method returns an iterable object like a list or a tuple, you can use the method call as the argument for the len() method by putting parentheses after the method name. For example,

class Particle:

    def __init__(self, name, charge, mass):

        self.name = name

        self.charge = charge

        self.mass = mass
    
    def get_info(self):

        return([self.name, self.charge, self.mass])

muon = Particle("Muon", -1, 105.7)

print(len(muon.get_info()))

This tutorial will detail the error and how to solve it with code examples.


TypeError: object of type ‘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 method is a function that belongs to an object of a class.

Methods are callable objects. 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.

class Particle:

    def __init__(self, name, charge, mass):

        self.name = name

        self.charge = charge

        self.mass = mass
    
    def get_info(self):

        return([self.name, self.charge, self.mass])

muon = Particle("Muon", -1, 105.7)

print(callable(muon.get_info))
True

Methods 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 get_info call returns a list, if we pass the get_info call to the callable() method, it will return False.

print(callable(muon.get_info()))
False

Example

Let’s look at an example of trying to get the length of a method. First, we will create a class which stores and returns attributes of different pizzas.

class Pizza:

    def __init__(self, name, price, is_vegetarian):

        self.name = name

        self.price = price

        self.is_vegetarian = is_vegetarian
    
    def get_info(self):

        return([self.name, self.price, self.is_vegetarian])

The __init__ method is the class constructor and sets the name, price and is_vegetarian attributes for the pizza.

The get_info method returns a list containing the pizza attributes.

Next, we will create an instance of the Pizza class containing attributes of the pepperoni pizza.

pepperoni = Pizza("pepperoni", 10.99, False)

Next, we will try to get the length of the list of attributes returned by the get_info method.

print(len(pepperoni.get_info))

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 17>()
     13         return([self.name, self.price, self.is_vegetarian])
     15 pepperoni = Pizza("pepperoni", 10.99, False)
---> 17 print(len(pepperoni.get_info))

TypeError: object of type 'method' has no len()

The error occurs because we did not call the method to return the list. Therefore, Python interprets the len() method call as trying to get the length of the method, 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_info and the object returned by the get_info() call.

print(type(pepperoni.get_info))
print(type(pepperoni.get_info()))
<class 'method'>
<class 'list'>

We can see that if we do not put parentheses after the method name, the object is a method. If we call the method we get the object the method returns, which in this case is a list.

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 method object and the list object using the built-in dir() method.

print('__len__' in dir(pepperoni.get_info))
print('__len__' in dir(pepperoni.get_info()))
False
True

We can see that __len__ is an attribute of the list object, which means we can use it as the argument for the len() method.

Solution

We can solve this error by calling the get_info method. We can call a method by putting parentheses () after the method name. Let’s look at the revised code:

class Pizza:

    def __init__(self, name, price, is_vegetarian):

        self.name = name

        self.price = price

        self.is_vegetarian = is_vegetarian
    
    def get_info(self):

        return([self.name, self.price, self.is_vegetarian])

pepperoni = Pizza("pepperoni", 10.99, False)

print(len(pepperoni.get_info()))

Let’s run the code to see the result:

3

We successfully called the get_info() method to return the list, and used the len() method to get the length of the list.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on the has no len() TypeErrors, go to the article:

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.

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

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.

Buy Me a Coffee