In this article, we will be discussing the TypeError: “int” Object is not callable exception. We will also be through solutions to this problem with example programs.
Why Is This Error Raised?
- “int” is used as a variable name.
- Arithmetic Operator not provided when calculating integers.
- Placing parentheses after a number
Using int As A Variable, Name
Variable declaration using in-built names or functions is a common mistake in rookie developers. An in-built name is a term with its value pre-defined by the language itself. That term can either be a method or an object of a class.
int
is an in-built Python keyword. As we discussed, it is not advisable to use pre-defined names as variable names. Although using a predefined name will not throw any exception, the function under the name will no longer be re-usable.
Let’s refer to the following example:
myNums = [56,13,21,54]
sum = 0
sum = sum(myNums)
print("sum of myNums list: ", sum)
Output and Explanation
- Variable
myNum
s is a list of 4 integers. - A variable
sum
is initialized with the value 0 - The sum of
myNums
list is calculated usingsum()
function and stored insum
variable. - Results printed.
What went wrong here? In step 2, we initialize a variable sum
with a value of 0. In Python, sum
is a pre-defined function. When were try to use the sum function in step 3, it fails. Python only remembers sum
as a variable since step 2. Therefore, sum()
has lost all functionality after being declared as a variable.
Solution
Instead of using sum
as a variable declaration, we can use more descriptive variable names that are not pre-defined (mySum
, mySum
, totalSum
). Make sure to follow PEP 8 naming conventions.
myNums = [56,13,21,54]
totalSum = 0
totalSum= sum(myNums)
print("sum of myNums list: ", totalSum)
Correct Output
sum of myNums list: 144
Arithmetic Operator Not Provided When Calculating Integers
Failing to provide an arithmetic operator in an equation can lead to TypeError: “int” object is not callable. Let’s look at the following example:
prices = [44,54,24,67]
tax = 10
totalPrice = sum(prices)
taxAmount = totalPrice(tax/100)
print("total taxable amounr: ", taxAmount)
Output / Explanation
- List of integers stored in the variable
prices
- Tax percentage set to
10
- Total price calculated and stored in
totalPrice
- Total Taxable amount calculated.
- Final result printed.
To calculate the taxable amount, we must multiply totalPrice
with tax percentage. In step 4, while calculating taxAmount
, the *
operator is missing. Therefore, this gives rise to TypeError: "int" Object Is Not Callable
Solution
Denote all operators clearly.
prices = [44,54,24,67]
tax = 10
totalPrice = sum(prices)
taxAmount = totalPrice*(tax/100)
print("total taxable amounr: ", taxAmount)
total taxable amount: 18.900000000000002
Recommended Reading | [Solved] TypeError: ‘str’ object is not callable
Placing Parentheses After an Integer
Let’s look at the following code:
1(2)
Output / Explanation
It is syntactically wrong to place parentheses following an integer. Similar to the previous section, It is vital that you ensure the correct operators.
Solution
Do not use brackets after a raw integer. Denote correct operators.
1 * 2
cursor.rowcount() TypeError: “int” Object Is Not Callable
Let’s look at the following code:
sample ="select * from myTable"
...
...
...
....
self.cur = self.con.cursor()
self.cur.execute(sample)
print(self.cur.rowcount())
Error Output
TypeError: 'int' object is not callable
Solution
According to the sqlite3 documentation provided by Python, .rowcount
is an attribute and not a function. Thereby remove the parenthesis after .rowcount
.
sample ="select * from myTable"
...
...
...
....
self.cur = self.con.cursor()
self.cur.execute(sample)
print(self.cur.rowcount)
TypeError: “int” object is not callable in OpenCV
Let’s refer to the following code.
contours,hierarchy = cv2.findContours(
thresh,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE
)
...
...
...
...
if (hierarchy.size() > 0):
numObj =hierarchy.size()
Error Output
if (hierarchy.size() > 0):
TypeError: 'int' object is not callable
Solution
The hierarchy
object returned is a numpy.ndarray
object. You should also note that the numpy.ndarray.size
attribute is an integer, not a method. Therefore, they cause the exception.
if event.type == pygame.quit() TypeError: ‘int’ Object Is Not Callable
Let’s refer to the example code to move an image:
import pygame
import sys
pygame.init()
...
...
...
while True:
for i in pygame.event.get():
if i.type() == pygame.QUIT:
sys.exit()
...
...
Error Output
if i.type() == pygame.QUIT:
TypeError: 'int' object is not callable
Solution
The condition statement should be:
if i.type == pygame.QUIT:
Instead of the current:
if i.type() == pygame.QUIT:
Please note that type
is a member of the class Event
, not a function. Therefore, it is not required to pass parenthesis.
TypeError: ‘int’ Object Is Not Callable Datetime
Let’s refer to the following code:
from datetime import *
...
...
...
for single_date in daterange(start_date, end_date):
if single_date.day() == 1 and single_date.weekday() == 6:
sundays_on_1st += 1
Error Output
TypeError: 'int' object is not callable
Solution
.day
is not a function but an attribute. Therefore passing parenthesis should be avoided.
from datetime import *
...
...
...
for single_date in daterange(start_date, end_date):
if single_date.day == 1 and single_date.weekday() == 6:
sundays_on_1st += 1
FAQs
You can fix this error by not using “int” as your variable name. This will avoid the cases where you want to convert the data type to an integer by using int().
Object not callable simply means there is no method defined to make it callable. Usually, parenthesis is used to call a function or object, or method.
Conclusion
We have looked at the exception TypeError: ‘int’ Object Is Not Callable. This error mostly occurs due to basic flaws in the code written. Various instances where this error appears have also been reviewed.