mardi 13 décembre 2022

One vs several objects - which is better OOP approach?

I am not sure if this question should be here or on another community on stackexchange. I have some problem regarding design. Here it is an simplified example of my code.

class Data:
store = {
    'key':[]
}
def __init__(self):
    self.arg1 = 'value'
    self.arg2 = 'value'
def add_to_store(contents):
   self.store['key'] += contents

Arguments arg1 and arg2 will always be the same while initializing object of class Data (not that important). Only store will change depending on contents of a file.

My dilemma is:

is it better to initialize an object in a for loop and each time working on the new one:

for file_content in files_contents:
   d = Data()
   d.add_to_store(file_content)

or should I create only one object and add method that will clear my dictionary?

d = Data()
for file_content in files_contents:
   d.add_to_store(file_content)

Which is better practice? or it depends and both are correct?

Aucun commentaire:

Enregistrer un commentaire