Solution For The Error “TypeError: ‘set’ object is not callable” in Python

Welcome back to the series of Python lessons. In this tutorial, I will help you to find the cause and fix the error “TypeError: ‘set’ object is not callable” in Python in a few ways, like using an if-else statement or editing variable names. Follow along and get it done.

What is the cause of the error “TypeError: ‘set’ object is not callable” in Python?

Set is a reasonably new data type that Python supports. It takes work to apply it to your project. Many programmers need to correct their mistakes when using this data type.

Many cases cause the “TypeError: ‘set’ object is not callable” error in Python. However, the most common is still due to naming functions and variables with the constructor set().

Here is an example

# Init set variable by constructor set()
set = set(['one', 'two', 'three'])

# Init a set with the same name
set([1, 2, 3])

With variable naming the same as the constructor name and set data type, it causes an error to overwrite the constructor, and your code gets an error like the above.

Traceback (most recent call last):
  File "D:\workspace\python\hello.py", line 5, in <module>
    set([1, 2, 3])
TypeError: 'set' object is not callable

If you name the variable set data type the same as any function name, you will also get this error.

def name():
    return 'Jone'

# Init sets name variable
name = {'Tommy', 'Jessica', 'Jane'}

# Get name 
print(name())

Now Python will misunderstand you are assigning the function name with a set, this type of overriding is unreasonable, so that, it will cause an error.

Output

Traceback (most recent call last):
  File "D:\workspace\python\hello.py", line 8, in <module>
    print(name())
TypeError: 'set' object is not callable

How to fix it?

Editing the variable name

Editing the variable name is the simplest way to fix this. Duplicating variables and assigning them indiscriminately, the possibility of making mistakes is very high.

You must ensure that when calling a set object, no variable or function has the same name at that time, which will confuse Python.

To fix the above error, I must correct the variable name.

def name():
    return 'Jone'

# Init setName
setName = {'Tommy', 'Jessica', 'Jane'}

# Log results
print('The value of name function: ' + name())
print('Values of setName: ', setName)

Output

The value of name function: Jone
Values of setName:  {'Tommy', 'Jessica', 'Jane'}

Quite simple and effective

Using if-else statement

In a project, you often have to set a lot of variables, and many times you have no control over them. For complex data types like the set, before initialization, we should check it with the if-else statement as follows:

def name():
    return 'Jone'

if name is None:
    name = {'Tommy', 'Jessica', 'Jane'}
else:
    print('Name has been defined, please change the name variable')

With simple code like this, you don’t need to worry about initializing variables with the same name. Every time you see a warning message, go back and correct the variable name.

Output

Name has been defined, please change the name variable

Summary

In the above article, I showed you how to fix the “TypeError: ‘set’ object is not callable” error in Python. Those are the most effective ways that you should follow. See you in the next tutorials.

Leave a Reply

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