Curly brackets in Python have a special meaning. They are used to denote a function invocation. If you specify a pair of curly brackets after an integer without an operator between them, Python thinks you’re trying to call a function. This will return an “TypeError: ‘int’ object is not callable” error.
In this guide, we talk about what this error means and why it is raised. We walk through two examples of this error to help you figure out what is causing it in your code.
TypeError: ‘int’ object is not callable
Python functions are called using curly brackets. Take a look at a statement that calls a function called “calculate_tip”:
calculate_tip(5, 10)
This function accepts two parameters. The values we have specified as parameters are 5 and 10. Because curly brackets have this special meaning, you cannot use them to call an integer.
The two most common scenarios where developers try to call an integer are when:
- The value of a function has been reassigned an integer value
- A mathematical operator is missing in a calculation
Let’s explore each of these scenarios one by one to help you fix the error you are facing.
Scenario #1: Function Has an Integer Value
Write a program that calculates the sum of all the tips the wait staff at a restaurant has received in a day. We start by declaring a list of tips and a variable which will store the cumulative value of those tips:
all_tips = [5, 10, 7.50, 9.25, 6.75] sum = 0
Next, we use the sum() method to calculate the total number of tips the wait staff have received:
sum = sum(all_tips) print("The total tips earned today amount to $" + str(sum) + ".")
The sum()
method adds up all the values in an array. We then print out a message to the console informing us how much money was earned in tips. We use the str()
method to convert the value of “sum” to a string so we can concatenate it to the string that contains our message.
Run our code:
Traceback (most recent call last): File "main.py", line 4, in <module> sum = sum(all_tips) TypeError: 'int' object is not callable
Our code returns an error because we have assigned a variable called “sum” which stores an integer value. Assigning this variable overrides the built-in sum()
method. This means that when we try to use the sum()
method, our code evaluates:
total_tips = 0([5, 10, 7.50, 9.25, 6.75])
We can fix this error by renaming the variable “sum”:
all_tips = [5, 10, 7.50, 9.25, 6.75] total_tips = 0 total_tips = sum(all_tips) print("The total tips earned today amount to $" + str(total_tips) +".")
We’ve renamed the variable “sum” to “total_tips”. Let’s run our code again:
The total tips earned today amount to $38.5.
Our code runs successfully!
Scenario #2: Missing a Mathematical Operator
Write a program that calculates a number multiplied by that number plus one. For instance, if we specify 9 in our program, it will multiply 9 and 10.
Start by asking a user to insert a number:
start_number = int(input("What number would you like to multiply? "))
Next, we multiply the value of the “start_number” variable by the number one greater than that value:
new_number = start_number (start_number + 1) print("{} multiplied by {} is {}.".format(start_number, start_number + 1, new_number))
Our code prints out a message informing us of the answer to our math question. Run our code and see what happens:
What number would you like to multiply? 9 Traceback (most recent call last): File "main.py", line 3, in <module> new_number = start_number (start_number + 1) TypeError: 'int' object is not callable
Our code does not finish executing. This is because we’ve forgotten an operator in our code. In our “new_number” line of code, we need to specify a multiplication operator. This is because Python treats square brackets followed by a value as a function call.
Add in a multiplication operator (*) to our code:
new_number = start_number * (start_number + 1)
Now, our code should work:
What number would you like to multiply? 10 10 multiplied by 11 is 110.
Our code returns the expected response.
Conclusion
The “TypeError: ‘int’ object is not callable” error is raised when you try to call an integer.
This can happen if you forget to include a mathematical operator in a calculation. This error can also occur if you accidentally override a built-in function that you use later in your code, like round()
or sum()
.
Now you’re ready to solve this common Python issue like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.