Select Page

How to Solve Python TypeError: cannot unpack non-iterable function object

by | Programming, Python, Tips

In Python, you can unpack iterable objects and assign their elements to multiple variables in the order they appear. If you try to unpack a function, you will throw the error TypeError: cannot unpack non-iterable function object. A function is not a sequence which we can loop over.

If the function returns an iterable object, you can call the function before performing unpacking. For example,

def get_ice_cream():

    return ['vanilla', 'strawberry', 'chocolate']

ice_cream_1, ice_cream_2, ice_cream_3 = get_ice_cream()
   

This tutorial will go through how to solve the error with code examples.


TypeError: cannot unpack non-iterable function object

TypeError occurs in Python when you perform an illegal operation for a specific data type. A function is a block of code which only runs when it is called and is not iterable. Unpacking is only suitable for iterable objects.

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.

Unpacking requires an iteration in order to assign values to variables in order, and as such requires iterable objects.

What is Unpacking in Python?

Unpacking is the process of splitting packed values into individual elements. The packed values can be a string, list, tuple, set or dictionary. During unpacking, the elements on the right-hand side of the statement are split into the values on the left-hand side based on their relative positions. Let’s look at the unpacking syntax with an example:

values = [10, 20, 30]

x, y, z = values

print(f'x: {x}, y: {y}, z: {z}')

The above code assigns the integer values in the value list to three separate variables. The value of x is 10, y is 20, and the value of z is 30. Let’s run the code to get the result:

x: 10, y: 20, z: 30

We can also unpack sets and dictionaries. Dictionaries are only ordered for Python version 3.7 and above but are unordered for 3.6 and below. Generally, it is not recommended to unpack unordered collections of elements as there is no guarantee of the order of the unpacked elements.

We cannot unpack an Integer because it is not an iterable object, and an iterable is a Python object that we can iterate over.

Example

Let’s look at an example of trying to unpack a function object.

First, we will define a function which returns a list containing the names of three ice creams.

def get_ice_cream():

    return ['vanilla', 'strawberry', 'chocolate']  

Next, we will try to unpack the list returned by the get_ice_cream function and print the values to the console.

ice_cream_1, ice_cream_2, ice_cream_3 = get_ice_cream

print(ice_cream_1)

print(ice_cream_2)

print(ice_cream_3)   

Let’s run the code to get the result:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [19], in <cell line: 5>()
      1 def get_ice_cream():
      3     return ['vanilla', 'strawberry', 'chocolate']
----> 5 ice_cream_1, ice_cream_2, ice_cream_3 = get_ice_cream
      7 print(ice_cream_1)
      9 print(ice_cream_2)

TypeError: cannot unpack non-iterable function object

The error occurs because we did not call the get_ice_cream function to return the list, therefore Python interprets this as trying to unpack the function object.

Solution

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

def get_ice_cream():

    return ['vanilla', 'strawberry', 'chocolate']

ice_cream_1, ice_cream_2, ice_cream_3 = get_ice_cream()

print(ice_cream_1)

print(ice_cream_2)

print(ice_cream_3)   

Let’s run the code to unpack the values and print them to the console:

vanilla
strawberry
chocolate

Summary

Congratulations on reading to the end of this tutorial!

For more reading on cannot unpack non-iterable object errors, go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!