Py学习  »  Python

如何修复Python错误:attribute error:type object X没有属性Y

ee94jrlc • 5 年前 • 1637 次点击  

我正在创建一个practice python文件,以便更好地理解面向对象编程,我得到以下错误:

AttributeError: type object 'ID' has no attribute 'the_other_number'

我真的不明白为什么会发生这种情况,因为当我像下面这样改变变量时,它工作得很好。我只在尝试在if语句中使用它时收到错误:

ID.the_other_number = 'new_value'

下面是我的示例代码,我正在试图修复,任何帮助都是感激的。

class ID():
   def __init__(self):
       self.other_number()
       pass

   def other_number(self):
       self.the_other_number = 3111

class ID_2():
    def __init__(self):
        self.update_number()

    def update_number(self):
       if ID.the_other_number > 4:
           print(ID.the_other_number)

if __name__ == '__main__':
   ID()
   ID_2()

我希望它能理解什么变量等于并正确运行if语句。另外,不要试图理解代码,我意识到代码没有意义,它只是一个例子。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51708
 
1637 次点击  
文章 [ 3 ]  |  最新文章 5 年前