Home > Net >  <Class> Object has no attribute <attribute_name>
<Class> Object has no attribute <attribute_name>

Time:11-19

So, i'm new to python and been stuck many hours facing an error when trying run the code bellow

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)

error: Traceback (most recent call last): File "", line 15, in File "", line 7, in addElement AttributeError: 'ConjuntoDeInteiros' object has no attribute 'conjunto'

What am i doing wrong???

CodePudding user response:

 The error that it shows you is caused because you are trying to add an instance of class ConjuntoDeInterios(), if you want to join the list of the other class with the current defined class you need to overload the operator on ConjuntoDeInterios() or simply accessing the list by this way:

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

    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)
    print(c1.union(c2.conjunto))
 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

CodePudding user response:

Your init function is wrong. It needs to be __init__ and you have _init_. Therefore your self.conjunto is never defined and hence your error. You also have conjunto and storage_capacity as parameters of __init__, but you don't pass them when creating ConjuntoDeInteiros().

CodePudding user response:

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
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Your code needs to define conjunto in self.conjunto. Otherwise it won't recognize conjunto.

  • Related