Summary: TypeError: ‘xyz’ object is not callable
is usually raised when you try to call a method with the same name as a variable in the code. To avoid this error, rename the variable and avoid using variables with names like built-in methods in Python.
Table of Contents
Have you come across TypeErrors as:
- TypeError: ‘int’ object is not callable
- TypeError: ‘float’ object is not callable
- TypeError: ‘str’ object is not callable
If the above errors keep bugging you then you have come to the right place to get rid of your problem. In this tutorial, we will learn about the TypeError: ‘xyz’ object is not callable
. Since any concept is best understood with the help of examples, we will walk through numerous scenarios and resolve the error in the process. So, without further delay,let’s dive into the discussion.
✨ Type 1: TypeError: ‘int’ object is not callable
➦ Scenario 1: Overriding a built-in function that you use later in your code as a variable
👉 If you declare a variable with the name int
and you also have a variable that accepts the user input with the help of the int()
method, then the Python is unable to distinguish between the inbuilt int()
method and the declared variable int
in the program.
Example 1: Suppose you are writing a program to swap the values of two variables using a third variable.
1 2 3 4 5 6 7 8 9 10 |
int = 0 a = int(input('Enter the first no.: ')) b = int(input('Enter the second no: ')) print("Before Swapping: a={} and b={}".format(a, b)) int = a a = b b = int print("After Swapping: a={} and b={}".format(a, b)) |
Output:
Solution: The error occurred because the third variable declared in the program has the name int
. To resolve the issue rename for the variable int
variable to something else, for example – temp
.
1 2 3 4 5 6 7 8 9 10 |
temp = 0 a = int(input('Enter the first no.: ')) b = int(input('Enter the second no: ')) print("Before Swapping: a={} and b={}".format(a, b)) temp = a a = b b = temp print("After Swapping: a={} and b={}".format(a, b)) |
Output:
Example 2:
1 2 3 4 5 6 7 |
a = 23 b = 45 round = 16 print(round((a/b)*0.9*round)) # Expected output: 7 |
Output:
Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/int object not callable.py”, line 4, in
print(round((a/b)*0.9*round))
TypeError: ‘int’ object is not callable
Solution: round()
is an in-built function in Python. So, when you declare a variable with the same name as round it leads to the occurrence of TypeError
. To avoid this error rename the round
variable to avoid conflict between the variable and function name.
1 2 3 4 5 6 |
a = 23 b = 45 c = 16 print(round((a/b)*0.9*c)) |
Output:
Exercise: Time to test your knowledge! Find the error in the program given below and rectify it to generate the output.
1 2 3 4 5 6 7 |
int = 5 a = int(input('enter a value: ')) for i in range(1, int): print(i * 5) # Expected Output when a = 5 is : 5 10 15 20 |
Solution:
➦ Scenario 2: Missing an Arithmetic Operator
If you accidentally miss a mathematical operator in a certain calculation within your code then it leads to TypeError: 'int' object is not callable
.
Example: The following code computes the Perimeter of a rectangle based of the formula P= 2(l + b)
.
1 2 3 4 5 6 |
l = int(input("Enter the length: ")) b = int(input("Enter the breadth: ")) p = 2(l+b) print("Perimeter of rectangle = ", p) |
Output:
Enter the length: 10
Enter the breadth: 5
Traceback (most recent call last):
File “main.py”, line 3, in
p = 2(l+b)
TypeError: ‘int’ object is not callable
Solution: In the above code we missed to enter the arithmetic operator * in line 3 which lead to the error. Simply include this operator to avoid the error.
1 2 3 4 5 6 7 |
l = int(input("Enter the length: ")) b = int(input("Enter the breadth: ")) p = 2*(l+b) print("Perimeter of rectangle = ", p) |
Output:
Enter the length: 10
Enter the breadth: 5
Perimeter of rectangle = 30
✨ Type 2: TypeError: ‘float’ object is not callable
The reasons behind the occurrence of TypeError: 'float' object is not callable
are similar, i.e.,
- Case 1: When you try to call a method when a property with the same name is available in code.
- Case 2: When you have forgotten to include a mathematical operator in the calculation.
Exercise: Find the error in the code given below and generate the output.
1 2 3 4 |
f = float(input("Enter temperature in Fahrenheit: ")) print(c,"°C") |
hint: Line 3 looks fishy!
Solution:
✨ Type 3: TypeError: ‘str’ object is not callable
Python throws TypeError: 'str' object is not callable
in two scenarios:
➦ Scenario 1: Declaring a Variable Named str along with the str() Method
Consider the example given below.
1 2 3 4 5 |
str = input('Enter your name: ') age = input('Enter your age: ') print(str(str+" is"+age+" years old.")) |
Output:
Enter your name: Shubham
Enter your age: 25
Traceback (most recent call last):
File “D:/PycharmProjects/pythonProject1/int object not callable.py”, line 3, in
print(str(str+” is”+age+” years old.”))
TypeError: ‘str’ object is not callable
Solution: Rename the str
variable.
1 2 3 4 5 |
name = input('Enter your name: ') age = input('Enter your age: ') print(str(name+" is "+age+" years old.")) |
Output:
Enter your name: Shubham
Enter your age: 25
Shubham is 25 years old.
➦ Scenario 2: Incorrect Usage of the % Operator while Formatting a String
If you forget to include the % operator to separate your string and the values you want to add to our string then it leads to the occurrence of TypeError: 'str' object is not callable
.
Example:
1 2 3 4 5 |
name = input('Enter your name: ') age = input('Enter your age: ') print("%s is %s years old."(name,age)) |
Output:
Enter your name: Shubham
Enter your age: 25
Traceback (most recent call last):
File “main.py”, line 3, in
print(“%s is %s years old.”(name,age))
TypeError: ‘str’ object is not callable
Solution: Ensure that you do not miss the % operator in your print statement.
1 2 3 4 5 6 |
name = input('Enter your name: ') age = input('Enter your age: ') print("%s is %s years old."%(name,age)) |
Output:
Enter your name: Shubham
Enter your age: 25
Shubham is 25 years old.
Conclusion
You might want to look at the article given to visualize errors that are similar to those mentioned in this article.
Read here: [Solved] TypeError: ‘Module’ Object Is Not Callable in Python?
Please subscribe and stay tuned for more interesting articles. Happy learning! 📚