TypeError: argument of type 'method' is not iterable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: argument of type 'method' is not iterable #

The Python "TypeError: argument of type 'method' is not iterable" occurs when we use the in or not in operators with a method but forget to call it. To solve the error, make sure to call the method.

typeerror argument of type method is not iterable

Here is an example of how the error occurs.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() # ⛔️ TypeError: argument of type 'method' is not iterable print('Alice' in emp.get_list) # 👈️ forgot to call method
We forgot to call the method with parentheses, e.g. emp.get_list(), so our code actually tries to check for membership in the method rather than in the list.

To solve the error, make sure to call the method.

main.py
class Employee(): def get_list(self): return ['Alice', 'Bob', 'Carl'] emp = Employee() print('Alice' in emp.get_list()) # 👉️ True print('Alice' not in emp.get_list()) # 👉️ False

We used parentheses to invoke the method, so now we check for membership in the list rather than the method.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

main.py
my_str = 'hello world' print('world' in my_str) # 👉️ True print('another' in my_str) # 👉️ False

x not in s returns the negation of x in s.

All built-in sequences and set types support the in and not in operators.

When used with a dictionary, the operators check for the existence of the specified key in the dict object.

Conclusion #

The Python "TypeError: argument of type 'method' is not iterable" occurs when we use the in or not in operators with a method but forget to call it. To solve the error, make sure to call the method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.