What does “TypeError 'xxx' object is not callable” means?

前端 未结 5 1424
梦谈多话
梦谈多话 2020-12-01 02:43

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 tel

相关标签:
5条回答
  • 2020-12-01 03:10

    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.

    0 讨论(0)
  • 2020-12-01 03:13

    The exception is raised when you try to call not callable object. Callable objects are (functions, methods, objects with __call__)

    >>> f = 1
    >>> callable(f)
    False
    >>> f()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not callable
    
    0 讨论(0)
  • 2020-12-01 03:18

    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
    >>>
    
    0 讨论(0)
  • 2020-12-01 03:26

    The action occurs when you attempt to call an object which is not a function, as with (). For instance, this will produce the error:

    >>> a = 5
    >>> a()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not callable
    

    Class instances can also be called if they define a method __call__

    One common mistake that causes this error is trying to look up a list or dictionary element, but using parentheses instead of square brackets, i.e. (0) instead of [0]

    0 讨论(0)
  • 2020-12-01 03:30

    I came across this error message through a silly mistake. A classic example of Python giving you plenty of room to make a fool of yourself. Observe:

    class DOH(object):
    def __init__(self, property=None):
        self.property=property
    
    def property():
        return property
    
    x = DOH(1)
    print(x.property())
    

    Results

    $ python3 t.py
    Traceback (most recent call last):
      File "t.py", line 9, in <module>
        print(x.property())
    TypeError: 'int' object is not callable
    

    The problem here of course is that the function is overwritten with a property.

    0 讨论(0)
提交回复
热议问题