TypeError:+:'function'和'int'的不支持的操作数类型...需要支持

时间:2017-10-10 08:33:35

标签: python

代码是

class Demo():
    count = 0
    def __init__(self,name, no):
        Demo.count = Demo.count + 1
        self.name = name
        self.no = no
    def display(self):
        print(self.name)
        print(self.no)
    def count(self):
        print(Demo.count)

D = Demo('Mohan',20)
D.display()
D.count()

当我运行此代码时,我收到以下错误消息。

  

TypeError:+:'function'和'int'不支持的操作数类型   处理以退出代码1完成

如何摆脱这个错误并使用类变量?

2 个答案:

答案 0 :(得分:0)

我猜你的变量(count)不能与你的方法(count)具有相同的名称

div

返回

class Demo():
    number = 0
    def __init__(self,name, no):
        Demo.number = Demo.number + 1
        self.name = name
        self.no = no
    def display(self):
        print (self.name)
        print(self.no)
    def count(self):
        print(Demo.number)
D = Demo('Mohan',20)
D.display()
D.count()

我刚刚更改了变量名称

答案 1 :(得分:0)

您只需重命名此功能即可使此代码正常工作:

def count(self):
      print(Demo.count)

或者这个变量:

count = 0

您可以在变量或函数名称中使用_(下划线)以避免冲突。

这一切都与命名有关所以当你尝试添加函数+ 1 Python解释器会引发错误。

相关问题