This error occurs when you try to iterate over a method object, for example, using a for
loop.
If your method returns an iterable object, you can solve the error by adding parentheses ()
after the method name to call it and return the object. 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) for attribute in muon.get_info(): print(attribute)
This tutorial will detail the error and how to solve it with code examples.
Table of contents
TypeError: ‘method’ object is not iterable
TypeError occurs in Python when you perform an illegal operation for a specific data type. A method is a function that belongs to an object of a class, and we cannot iterate over it.
What is an Iterable Object in Python?
An iterable is an object that can be “iterated over“, for example in a for
loop. In terms of dunder methods under the hood, an object can be iterated over with “for
” if it implements __iter__()
or __getitem__()
.
An iterator returns the next
value in the iterable object. An iterable generates an iterator when it is passed to the iter()
method.
In terms of dunder methods under the hood, an iterator is an object that implements the __next__()
method.
A for loop automatically calls the iter()
method to get an iterator and then calls next
over and over until it reaches the end of the iterable object.
Example
Let’s look at an example of trying to iterate over a method.
First, we will create a class which stores and returns attributes of fundamental physics particles.
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])
The __init__
method is the class constructor and sets the name
, charge
and mass
attributes for the particle.
The get_info
method returns a list containing the particle attributes.
Next, we will create an instance of the Particle
class containing attributes of the muon
particle.
muon = Particle("Muon", -1, 105.7)
Next, we will try to iterate over the attributes returned by the get_info
method.
for attribute in muon.get_info: print(attribute)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [16], in <cell line: 13>() 9 return([self.name, self.charge, self.mass]) 11 muon = Particle("Muon", -1, 105.7) ---> 13 for attribute in muon.get_info: 14 print(attribute) TypeError: 'method' object is not iterable
The error occurs because we did not call the get_info
method. Therefore Python interprets the for
loop as trying to iterate over the method object, which is not iterable.
Solution
We can solve this error by calling the method get_info
. We can call a method by specifying the method name and putting parentheses after the name. Let’s look at the revised code.
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) for attribute in muon.get_info(): print(attribute)
Let’s run the code to get the attributes of the muon particle:
Muon -1 105.7
Summary
Congratulations on reading to the end of this tutorial!
For more reading on not iterable TypeErrors, go to the articles:
- How to Solve Python TypeError: ‘int’ object is not iterable
- How to Solve Python TypeError: ‘builtin_function_or_method’ object is not iterable
- How to Solve Python TypeError: ‘function’ object is not iterable
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
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.