How to Solve Python TypeError: ‘set’ object is not callable

by | Programming, Python, Tips

This error occurs when you try to call a set object by putting parentheses () after it like a function. Only functions respond to function calls.

You can solve this error by ensuring you do not override the name for the built-in function set. For example,

my_list = [2, 4, 4, 5, 7,7, 10, 10, 1, 2]

my_set = set(my_list)

print(my_set)

This tutorial will go through the error in detail and how to solve it with the help of code examples.


TypeError: ‘set’ object is not callable

Calling a function means the Python interpreter executes the code inside the function. In Python, we can only call functions. We can call functions by specifying the name of the function we want to use followed by a set of parentheses, for example, function_name(). Let’s look at an example of a working function that returns a string.

# Declare function

def simple_function():

    print("Learning Python is fun!")

# Call function

simple_function()
Learning Python is fun!

We declare a function called simple_function in the code, which prints a string. We can then call the function, and the Python interpreter executes the code inside simple_function().

Sets do not respond to a function call because they are not functions. If you try to call a set object as if it were a function, you will raise the TypeError: ‘set’ object is not callable.

We can check if an object is callable by passing it to the built-in callable() method. If the method returns True, then the object is callable. Otherwise, if it returns False the object is not callable. Let’s look at evaluating a set object with the callable method:

lst = [4, 9, 1, 1, 2, 3, 2]

my_set = set(lst)

print(type(my_set))

print(callable(my_set))
<class 'set'>
False

The callable function returns False for the set object.

Example

Let’s look at an example of attempting to call a set object. First, we will create a list of strings and then convert the set to a list.

my_lst = ["car", "car", "lorry", "bike", "train", "bike"]

set = set(my_lst)

print(set)
{'bike', 'train', 'lorry', 'car'}

Next, we will try to create another set from a list:

shapes_lst = ["square", "circle", "triangle", "square", "circle"]

shape_set = set(my_lst)

print(shape_set)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [12], in <cell line: 3>()
      1 shapes_lst = ["square", "circle", "triangle", "square", "circle"]
----> 3 shape_set = set(my_lst)
      5 print(shape_set)

TypeError: 'set' object is not callable

The error occurs because we assigned the first set object to the variable name set, which overrides reserved name set for the built-in function. Then, when we try to create a new set, we are calling the set object instead.

We can verify the object type using the built-in type() function.

print(type(set))
<class 'set'>

The set variable holds a set object.

Solution

We can solve the error by deleting the variable set using del, then recreate the set object with a different name that is not reserved for built-in functions.

del set
my_lst = ["car", "car", "lorry", "bike", "train", "bike"]

vehicle_set = set(my_lst)

print(vehicle_set)

Let’s run the code to get the first set.

{'bike', 'train', 'lorry', 'car'}

Next, we can create a new set using the built-in function set() because we did not override it.

shapes_lst = ["square", "circle", "triangle", "square", "circle"]

shape_set = set(shapes_lst)

print(shape_set)

Let’s run the code to get the result.

{'square', 'triangle', 'circle'}

Summary

Congratulations on reading to the end of this tutorial!

For further reading on not callable TypeErrors, go to the articles:

To learn more about Python, specifically for data science and machine learning, go to the online courses page on Python.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

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.

Buy Me a Coffee