TypeError: ‘int’ object is not callable

Estimated reading time: 1 minute

We have posted several python solutions to TypeErrors here on the website. Here is another one we hope you will find useful in solving a programming issue you may have.

So what does the error mean?

In the below code, we have four variables that have been assigned an integer.

a= 10
b= 11
c= 12
int= 13

d = int(a/b*c)
print(d)

As can be seen we also have a variable d that is assigned to a function int , that is using the variables a,b,c.

As int is a function it cannot be assigned as a variable, and for this reason the below error will appear:

d = int(a/b*c)
TypeError: 'int' object is not callable

So how can this be fixed?

The solution to this is quite straight forward, it is important not to assign a function as a variable. The fix you would apply is as follows:

Incorrect code:

a= 10
b= 11
c= 12
int = 13  ===> change this line to fix the error

d = float(a/b*c)
print(d)

Corrected code:
a= 10
b= 11
c= 12
int_value = 13  ===> corrected line of code
d = float(a/b*c)
print(d)

Giving you the result:
10

As can also be seen with TypeError: ‘str’ object is not callable assign variables to functions should be avoided at all cost.

TypeError: cannot unpack non-iterable int object

Often when working on a data analytics project it requires you to split data out into its constituent parts.

There are a number of reasons for this, it can be confusing when you get errors as with the title of this post.

Before we explain this error and what it means, lets us first explain unpacking

Unpacking basically means splitting something up into a number of its parts that make it up.

To demonstrate if you take a,b,c = 123, and look to unpack it, it throws out the error, but why?

Well pure and simple, we have three values on the left “a,b,c”, looking for three values on the right.

a,b,c = 123
print(a)

Output:
 a,b,c = 123
TypeError: cannot unpack non-iterable int object

If you would like to fix this problem change the right hand side to have three values.

a,b,c = 1,2,3
print(a)
print(b)
print(c)
print(a,b,c)

Output:
1
2
3
1 2 3

Process finished with exit code 0

In essence, what is going on is that an evaluation checking that both sides have the same amount of values.

It is important to remember, the code above we used to show the error is an integer, which cannot be unpacked.

So if you take 123 for an example, which we used here it cannot be split into say 100 and 10 and 13.

In this case, even though when they are added up to 123, integers cannot be unpacked.

For this reason in the code for our solution, the difference is that the values used are tuples as follows:

a,b,c = 1,2,3
print(a)
print(b)
print(c)
print(a,b,c)
print(type((a,b,c)))

Or 
a,b,c = (1,2,3)
print(a)
print(b)
print(c)
print(a,b,c)
print(type((a,b,c)))

yield the same result:

1
2
3
1 2 3
<class 'tuple'>

Process finished with exit code 0

So in summary:

When unpacking there are a number of things to remember:

  • Integers on their own cannot be unpacked.
  • You need to make sure that if you have a number of variables, that you have the same number of integers if they the values.
    • This will make it a tuple and unpacking can then happen.