<Class> 对象没有属性 <attribute_name>

<Class> Object has no attribute <attribute_name>

所以,我是 python 的新手,在尝试 运行 下面的代码

时遇到错误被困了好几个小时
class ConjuntoDeInteiros:
    def _init_(self, conjunto, storage_capacity=100): #initializes a list and its length, default value is 100
        self.storage_capacity = storage_capacity
        self.conjunto = [None] * storage_capacity

    def addElement(self, intNumber): #add an element to the list
        self.conjunto.append(intNumber)

    def union(self, instanceOfConjuntoDeInteiros): #returns the union of an instance list with another instance list
        return self.conjunto + instanceOfConjuntoDeInteiros

if __name__ == '__main__':
    c1 = ConjuntoDeInteiros()
    c2 = ConjuntoDeInteiros()
    c1.addElement(10)
    c2.addElement(5)
    c1.union(c2)

错误:回溯(最近调用最后): 文件“”,第 15 行,位于 文件“”,第 7 行,在 addElement 中 AttributeError: 'ConjuntoDeInteiros' 对象没有属性 'conjunto'

我做错了什么???

class ConjuntoDeInteiros:
    def _init_(self, conjunto, storage_capacity=100): #initializes a list and its length, default value is 100
        self.storage_capacity = storage_capacity
        self.conjunto = [None] * storage_capacity

您的代码需要在 self.conjunto 中定义 conjunto。否则无法识别conjunto。

您的 init 函数有误。它需要 __init__ 而你有 _init_。因此,您的 self.conjunto 从未定义,因此您的错误。你也有 conjuntostorage_capacity 作为 __init__ 的参数,但是你在创建 ConjuntoDeInteiros().

时没有传递它们

它显示的错误是因为您试图添加 class ConjuntoDeInterios() 的实例,如果您想加入 列表 其他 class 与当前定义的 class 您需要通过这种方式在 ConjuntoDeInterios() 上重载 operator+

class ConjuntoDeInteiros:
    def __init__(self, conjunto=[None], storage_capacity=100): #initializes a list and its length, default value is 100
        self.storage_capacity = storage_capacity
        self.conjunto = [None] * storage_capacity

    # overload operator (+)
    def __add__(self, instanceOfConjuntoDeInteiros): # returns the union of an instance list with another instance list
        return self.conjunto + instanceOfConjuntoDeInteiros.conjunto

    def addElement(self, intNumber): # add an element to the list
        self.conjunto.append(intNumber)

if __name__ == '__main__':
    c1 = ConjuntoDeInteiros()
    c2 = ConjuntoDeInteiros()
    c1.addElement(10)
    c2.addElement(5)
    print(c1 + c2)
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 None, None, 10,   None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, None, None, None, None, None, None, None, None, None, 
 None, None, None, None, None, 5