Steps to fix the TypeError: ‘method’ object is not iterable in Python

TypeError: 'method' object is not iterable in Python

OOP in Python is helpful, but you must have basic OOP knowledge before using it. If you have no experience with OOP, a TypeError: ‘method’ object is not iterable in Python can occur when trying to iterate over an object’s attribute using a for loop, and you don’t understand why.

Why does the TypeError: ‘method’ object is not iterable in Python occur?

When this error occurs, it is usually because we are trying to iterate through a method instead of an iterable object. Methods are not iterable, so this causes an error. To fix this, we must ensure that we are only trying to iterate through iterable objects, not methods.

Here are some examples of what causes the error.

Missing parentheses when calling the method

When you call a method on an object but forget to include the parentheses (), Python will return the method itself instead of what it returns. That can cause an error. See the example below for more information.

class Website:
  learn = 'learnshareit.com'
  search = 'google.com'
  video = 'youtube.com'

  def getAll(self):
    return [self.learn, self.search, self.video]

web = Website()

# Want to iterate through the return value of the getAll method but forgot the '()'
for item in web.getAll:
  print(item)

Error:

TypeError: 'method' object is not iterable

The name of the variable and the method are identical

If a property and a method have the same name, Python will call the method instead of the property when using For loop. For example, if we have a class with a property called allWeb and a method called allWeb(), and we call ‘object.allWeb‘ within a For loop, we will be iterating over the allWeb() method instead of the allweb property.

class Website:
  learn = 'learnshareit.com'
  search = 'google.com'
  video = 'youtube.com'
  allWeb = [learn, search, video]

  def allWeb(self):
    return self.allWeb

web = Website()

# Iterating over the allWeb() method instead of iterating over the allWeb property
for item in web.allWeb:
  print(item)

Error:

TypeError: 'method' object is not iterable

How to fix this error?

Call the method correctly

To fix this error, remember that a method is only really executed when including a pair of parentheses. So if you want to iterate over the return value of a method, actually call the method correctly. Like this:

class Website:
  learn = 'learnshareit.com'
  search = 'google.com'
  video = 'youtube.com'

  def getAll(self):
    return [self.learn, self.search, self.video]

web = Website()

# Call the getAll() method correctly with parentheses ()
for item in web.getAll():
  print(item)

Output:

learnshareit.com
google.com
youtube.com

Avoid using the same variable and method names

In Python, we should avoid using the same variable name as the method name when using OOP. Let’s change the function name allWeb() to getAllWeb() to prevent the error.

class Website:
  learn = 'learnshareit.com'
  search = 'google.com'
  video = 'youtube.com'
  allWeb = [learn, search, video]

  # Use a different name than the allWeb property above
  def getAllWeb(self):
    return self.allWeb

web = Website()

for item in web.allWeb:
  print(item)

Output:

learnshareit.com
google.com
youtube.com

Summary

Congratulations! You have just fixed the TypeError: ‘method’ object is not iterable in Python. This error is a bit tricky, but with some knowledge of object-oriented programming, you can quickly fix it.

Happy coding!

Maybe you are interested:

Leave a Reply

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