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

前端 未结 5 1431
梦谈多话
梦谈多话 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.

提交回复
热议问题