TypeError: 'int' object is not callable

Question:

Given the following:

a = 23
b = 45
c = 16

round((a/b)*0.9*c)

Running the above outputs an error:

TypeError: 'int' object is not callable.

How can I round the output to an integer?

Asked By: rob

||

Answers:

Stop stomping on round somewhere else by binding an int to it.

Somewhere else in your code you have something that looks like this:

round = 42

Then when you write

round((a/b)*0.9*c)

that is interpreted as meaning a function call on the object bound to round, which is an int. And that fails.

The problem is whatever code binds an int to the name round. Find that and remove it.

Answered By: David Heffernan

I got the same error (TypeError: ‘int’ object is not callable)

def xlim(i,k,s1,s2):
   x=i/(2*k)
   xl=x*(1-s2*x-s1*(1-x)) / (1-s2*x**2-2*s1*x(1-x))
   return xl 
... ... ... ... 

>>> xlim(1,100,0,0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in xlim
TypeError: 'int' object is not callable

after reading this post I realized that I forgot a multiplication sign * so

def xlim(i,k,s1,s2):
   x=i/(2*k)
   xl=x*(1-s2*x-s1*(1-x)) / (1-s2*x**2-2*s1*x * (1-x))
   return xl 

xlim(1.0,100.0,0.0,0.0)
0.005

tanks

Answered By: Tomate

In my case I changed:

return <variable>

with:

return str(<variable>)

try with the following and it must work:

str(round((a/b)*0.9*c))
Answered By: Jonathan Arias

I was also facing this issue but in a little different scenario.

Scenario:

param = 1

def param():
    .....
def func():
    if param:
        var = {passing a dict here}
        param(var)

It looks simple and a stupid mistake here, but due to multiple lines of codes in the actual code, it took some time for me to figure out that the variable name I was using was same as my function name because of which I was getting this error.

Changed function name to something else and it worked.

So, basically, according to what I understood, this error means that you are trying to use an integer as a function or in more simple terms, the called function name is also used as an integer somewhere in the code.
So, just try to find out all occurrences of the called function name and look if that is being used as an integer somewhere.

I struggled to find this, so, sharing it here so that someone else may save their time, in case if they get into this issue.

Answered By: Sugandha Jain

As mentioned you might have a variable named round (of type int) in your code and removing that should get rid of the error. For Jupyter notebooks however, simply clearing a cell or deleting it might not take the variable out of scope. In such a case, you can restart your notebook to start afresh after deleting the variable.

Answered By: krishnakeshan

Sometimes the problem would be forgetting an operator while calculation.

Example:
print(n-(-1+(math.sqrt(1-4(2*(-n))))/2)) rather
it has to be
print(n-(-1+(math.sqrt(1-4*(2*(-n))))/2))

HTH

Answered By: Mebatsion Sahle

There are two reasons for this error "TypeError: ‘int’ object is not callable"

  1. Function Has an Integer Value

Consider

a = [5, 10, 15, 20]
max = 0
max = max(a)
print(max)

This will produce TypeError: ‘int’ object is not callable.

Just change the variable name "max" to var(say).

a = [5, 10, 15, 20]
var = 0
var = max(a)
print(var)

The above code will run perfectly without any error!!

  1. Missing a Mathematical Operator

Consider

a = 5
b = a(a+1)
print(b)

This will also produce TypeError: ‘int’ object is not callable.

You might have forgotten to put the operator in between ( ‘*’ in this case )

Answered By: Shambhav Agrawal

You can always use the below method to disambiguate the function.

__import__('__builtin__').round((a/b)*0.9*c)

__builtin__ is the module name for all the built in functions like round, min, max etc. Use the appropriate module name for functions from other modules.

Answered By: Nithish Thomas

I encountered this error because I was calling a function inside my model that used the @property decorator.

@property
def volume_range(self):
    return self.max_oz - self.min_oz

When I tried to call this method in my serializer, I hit the error "TypeError: ‘int’ object is not callable".

    def get_oz_range(self, obj):
      return obj.volume_range()

In short, the issue was that the @property decorator turns a function into a getter. You can read more about property() in this SO response.

The solution for me was to access volume_range like a variable and not call it as a function:

    def get_oz_range(self, obj):
      return obj.volume_range # No more parenthesis
Answered By: Banjoe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.