-1

Ever since I've learned the basics of Python classes I've had a single question on my mind: How do you automate the creation of objects instead of having to type test1 = class(example, test) every time? I've tried using a static function within the class to create the function as such:

class test:
       num_of_t = 0
       coomers = []
       def __init__(self, tes, t):
              self.test = tes
              self.t = t
              test.num_of_t += 1
       @staticethod
       def creat():
              example_input = input("test:")
              example_input2 = input("test2:")
              test.coomers.append("test_{}".format(test.num_of_t))
              for x in test.coomers:
                     x = test(example_input, example_input2)

def starter():
       while y<10:
              y += 1
              test.creat()

What the program is supposed to do is create 10 objects "test_1, test_2, test_3, etc" respectively with their own values for tes and t because of the 2 inputs. My problem is in x = test(example_input, example_input2) as it won't create the class as one would by typing test_1 = test(3, 7)

Nachtel
  • 11
  • 2
  • The name of the variable can ONLY be given by writing it in the code, no other way, that's it not a part of the object, just it's name in the code – azro Dec 01 '19 at 10:22

1 Answers1

0

It would be better if you use a dictionary to save the created instances of the class like this :

def starter():
    key_list = [f'test_{i+1}' for i in range(10)]
    instance_dict = {}
    fixed_tes = 3
    fixed_t = 7
    for k in key_list:
        instance_dict[k] = test(fixed_tes, fixed_t)
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56