Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "TypeError 'xxx' object is not callable" means?

Tags:

python

As a starting developer in Python I've seen this error message many times appearing in my console but I don't fully understand what does it means.

Could anyone tell me, in a general way, what kind of action produces this error?

like image 488
prl900 Avatar asked Jan 24 '14 05:01

prl900


People also ask

How do I fix this object is not callable?

The Python "TypeError: object is not callable" occurs when we try to call a not-callable object (e.g. a list or dict) as a function using parenthesis () . To solve the error, make sure to use square brackets when accessing a list at index or a dictionary's key, e.g. my_list[0] .

How do I fix type error int object is not callable?

How to resolve typeerror: 'int' object is not callable. To resolve this error, you need to change the name of the variable whose name is similar to the in-built function int() used in the code.

Why is my object not callable Python?

The Python "TypeError: 'int' object is not callable" occurs when we try to call an integer value as a function. To solve the error, correct the assignment, make sure not to override the built-in int() function, and resolve any clashes between function and variable names.

How do I fix string is not callable?

The Python "TypeError: 'str' object is not callable" occurs when we try to call a string as a function, e.g. by overriding the built-in str() function. To solve the error, make sure you're not overriding str and resolve any clashes between function and variable names.


2 Answers

That error occurs when you try to call, with (), an object that is not callable.

A callable object can be a function or a class (that implements __call__ method). According to Python Docs:

object.__call__(self[, args...]): Called when the instance is “called” as a function

For example:

x = 1
print x()

x is not a callable object, but you are trying to call it as if it were it. This example produces the error:

TypeError: 'int' object is not callable

For better understaing of what is a callable object read this answer in another SO post.

like image 119
Christian Tapia Avatar answered Oct 05 '22 01:10

Christian Tapia


The other answers detail the reason for the error. A possible cause (to check) may be your class has a variable and method with the same name, which you then call. Python accesses the variable as a callable - with ().

e.g. Class A defines self.a and self.a():

>>> class A:
...     def __init__(self, val):
...         self.a = val
...     def a(self):
...         return self.a
...
>>> my_a = A(12)
>>> val = my_a.a()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>>
like image 16
wmorrison365 Avatar answered Oct 05 '22 01:10

wmorrison365