I am new to OOP and Python in general and having the following problem: Suppose I have a file containing the data line by line that I want to use to create class instances. What is the most pythonic way of processing the file, initializing instances and storing them? I have 3 alternative methods: CASE-1: class method that processes the whole file line by line, creating instances and storing them in a list CASE-2: function outside of the class doing the same as CASE-1 CASE-3: function outside of the class processing each line individually and a for loop in main e.g:
class TestClass:
all_instances_1 = []
def __init__(self, var1, var2):
self.var1 = var1
self.var2 = var2
#CASE-1
@classmethod
def from_file(cls, input_file):
my_file = open(input_file, "r")
for line in my_file:
var1, var2 = line.split(',')
new_instance = TestClass(var1, var2])
cls.all_instances_1.append(new_instance)
my_file.close()
#CASE-2
def outside_class_init(input_file):
all_instances_2 = []
my_file = open(input_file, "r")
for line in my_file:
var1, var2 = line.split(',')
new_instance = TestClass(var1, var2])
all_instances_2.append(new_instance)
my_file.close()
return all_instances_2
#CASE-3
def one_by_one(my_str):
var1, var2 = line.split(',')
new_instance = TestClass(var1, var2])
return new_instance
def main():
filename = "$FILE"
TestClass.from_file(filename)
instances_list2 = outside_class_init(thefile)
all_instances_3 = []
my_file = open(thefile, "r")
for line in my_file:
all_instances_3.append(one_by_one(line))
my_file.close()
if __name__ == '__main__':
main()
If none of them is truly a good approach how would you handle it? Thank you in advance