lundi 11 octobre 2021

Generate random objects by modifying its' attribute inside __init__ function using a for loop

I'm quite new to object-oriented programming and I'm trying to create random objects by using the Agent() class:

class Agent():


def __init__(self, r = None):
    
    self.model = sa.Anm(model_info)
    
    if r is None:
        for n in range(len(self.model.elems)):
            self.model.elemsSections[n] = np.random.choice(self.model.sections[:,0])
            
    self.fitness = 0
    
    self.model.drawModel()

The self.model attribute reads the model_info .py file containing a "template" with the model's variables. Then, the for loop modifies the model.elemsSections attribute with a random choice of available sections inside the model.sections attribute.

Problem is, as I create the objects, the last instantiation modifies the ones created before. The closest I could get to answer this was in this question here, but with no results.

Maybe it's something with the for loop inside the __init__ function? Somehow, the last for loop executed is applied to the objects created before.

Thanks!




Aucun commentaire:

Enregistrer un commentaire