Solve - TypeError: 'set' object is not callable in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

Solve - TypeError: 'set' object is not callable in Python #

The Python "TypeError: 'set' object is not callable" occurs when we try to call a set object as a function, e.g. by overriding the built-in set() function. To solve the error, make sure you're not overriding set and resolve any clashes between function and variable names.

typeerror set object is not callable

Here is one example of how the error occurs.

main.py
# 👇️ overriding built-in set() set = set(['a', 'b', 'c']) # ⛔️ TypeError: 'set' object is not callable set(['d', 'e', 'f'])

We override the built-in set() function setting it to a set object and try to call it as a function.

To solve the error, make sure to rename your variable and restart your script.

main.py
# ✅ Not overriding built-in set() anymore my_set = set(['a', 'b', 'c']) print(set(['d', 'e', 'f'])) # 👉️ {'d', 'f', 'e'}

Another common cause of the error is simply trying to call a set as a function.

main.py
my_set = {'a', 'b', 'c'} # ⛔️ TypeError: 'set' object is not callable my_set()

To solve the error, you either have to remove the parenthesis or figure out how the variable got assigned a set instead of a function or a class.

Make sure you don't have a function and a variable with the same name.

main.py
def example(): return 'hello world' example = {'a', 'b', 'c'} # ⛔️ TypeError: 'set' object is not callable print(example())
The example variable shadows the function with the same name, so when we try to call the function, we actually end up calling the variable.

Renaming the variable or the function solves the error.

The error is also caused when we have a class method and a class property with the same name.

main.py
class Employee(): def __init__(self, tasks): # 👇️ this attribute hides the method self.tasks = tasks # 👇️ same name as class variable def tasks(self): return self.tasks emp = Employee({'dev', 'test'}) # ⛔️ TypeError: 'set' object is not callable print(emp.tasks())

The Employee class has a method and an attribute with the same name.

The attribute hides the method, so when we try to call the method on an instance of the class, we get the object is not callable error.

To solve the error, you have to rename the class method.

main.py
class Employee(): def __init__(self, tasks): self.tasks = tasks def get_tasks(self): return self.tasks emp = Employee({'dev', 'test'}) print(emp.get_tasks()) # 👉️ {'test', 'dev'}

Once you rename the method, you will be able to call it without any issues.

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.