1

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???

1
  • 1
    _init_ -> __init__, use two underscores on each side Commented Nov 18, 2021 at 22:44

3 Answers 3

3

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().

Sign up to request clarification or add additional context in comments.

4 Comments

Not really necessary because the Python constructor works in that way.
@jezza_99 Thank you!!! It worked! I didn't noticed the typo ahaha
@h.devillefletcher I understand that you can call parameter=value in __init__, but conjunto still requires a user parameter as it isn't defined
that`s right! and might not be a necessary parameter in this case...
2

 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() 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

    # 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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.