I need to make multiple instances of a class, then use its methods to find the output. It is straightforward to write the code in multiple lines but I need a more efficient way of doing that. Basically, here is what I want to do in a loop:

class test(object):
    ...
    def job(self,x='hi',y=4):
        return y+len(x)

A1 = test()
y1 = A1.job(x = 'word_1',y=12)
A2 = test()
y2 = A2.job(x = 'word_2',y=5)
A3 = test()
y3 = A3.job(x = 'word_3',y=-12)
A4 = test()
y4 = A4.job(x = 'word_4',y=1)
A5 = test()
y5 = A5.job(x = 'word_5',y=120)
A6 = test()
y6 = A6.job(x = 'word_6',y=-1)
A7 = test()
y7 = A7.job(x = 'word_7',y=10)

You must use lists for this

A = []
Y = []
val = [12, 5, -12, 1, 120, -1, 10]
for i, v in enumerate(val):
    t = test()
    name = 'word_{}'.format(i)
    j = t.job(x=name, y=v)
    A.append(t)
    Y.append(j)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.