I'm Python beginner. I'm trying to write a loop over a class and compare different results. Results are produced in a method called job. Then in each loop I want to run this method of the class. The last line of the code does not work. What is the possible solution?

Here is an example:

class test:
   def __init__(self,a)
       ...
   def job(self,x):
        ...

a = 5  # arbitrary numebr
instance_names = ['model_1', 'model_2', 'model_3']
for i, name in enumerate(instance_names):
    model_name = 'CC' + name
    model_job = model_name + '.job'
    vars()[model_name] = test(a)
    model_job(i)
Member Avatar

Should 9 be for i, name in enumerate(class_names) ?

commented: yes, it is a typo, thanks for catching the typo +0