Steps to fix the “Argument of type builtin_function_or_method is not iterable” error in Python

Argument of type builtin_function_or_method is not iterable

Whether you’re a beginner, experienced, or somewhere in between, you probably have the occasional silly mistake while coding. One of the dumbest mistakes that many developers have reported could be the “Argument of type builtin_function_or_method is not iterable” error. Today, we’ll show you how to fix this error quickly.

When does the “Argument of type builtin_function_or_method is not iterable” error appear?

When using the Membership operators in conjunction with a method or function without the parentheses, Python treats it as an iterable object. However, built-in functions and methods are not designed to be used this way. As a result, the error “Argument of type builtin_function_or_method is not iterable” appears. Look at the example below to see how this error appears.

courses = {
    "name": "Python for beginners",
    "price": 0,
    "owner": "LearnShareIT",
}

# Using the in operator with the 'values' function but forgetting to include the round brackets
print("LearnShareIT" in courses.values)

Error:

TypeError: argument of type 'builtin_function_or_method' is not iterable

This error can also occur when using the not in operator because not in is also a Membership operator.

How to fix this error?

Here are a few solutions for correcting and avoiding this error in the future. Read them all and choose the best one for you.

Call the function correctly

Functions are designed to perform a single operation before returning a value. To make the function work, add round brackets to the end of the function name. For example, to make the values function in our example work, add round brackets to the end of the values. Like this:

courses = {
    'name': 'Python for beginners',
    'price': 0,
    'owner': 'LearnShareIT',
}

# Using the in operator with the values() function
print('LearnShareIT' in courses.values())

Output:

True

Make sure the object is iterable

Since the Membership operators require an iterable object, make sure the object you’re using is an iterable object before using the Membership operator.

To make sure an object is iterable, you can use the iter() function and try…except block.

The iter() function allows you to create an iterator from an iterable object. If the object passed to iter() is not an iterable object, Python will throw a TypeError, which we will handle using the try…except block. Let’s apply this idea to our case to ensure that our object is iterable.

courses = {
    'name': 'Python for beginners',
    'price': 0,
    'owner': 'LearnShareIT',
}

try:
    # If courses.values is iterable, the try block code will be executed
    iter(courses.values)
    print('LearnShareIT' in courses.values)
except TypeError:
    # If the code in the except block executes, courses.values is not iterable
    print("The object you're using is not iterable. Please check again!")

Output:

The object you're using is not iterable. Please check again!

As you can see, the error has been replaced by a message line that you created.

Summary

To summarize, this error is easy to correct but also easy to make. So, if you encounter this error in the future, carefully read the error message that Python throws, and then follow the steps in this article to remove this error from your program.

Have a beautiful day!

Maybe you are interested:

Leave a Reply

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