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

前端 未结 5 1433
梦谈多话
梦谈多话 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: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 
        print(x.property())
    TypeError: 'int' object is not callable
    

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

提交回复
热议问题