This solution helps to create several instances of a class using a for loop as well as the globals() function.
class Cir:
def __init__(self, name):
self.name = name
This code defines a class called Cir with an __init__ method that takes a single argument name and assigns it to the object's name attribute.
for i in range(5):
inst_name = "my_instance_" + str(i)
globals()[inst_name] = Cir(inst_name)
This loop creates five instances of the Cir class and assigns them to global variables with names that depend on the value of the loop variable i. The variable inst_name is created as a string that adds the constant string my_instance_ with the string representation of the loop variable i. Then, the globals() function returns a dictionary of the global symbol table, and the resulting dictionary is accessed using inst_name as a key to add a new global variable with the name specified in inst_name and the value of the newly created instance of the Cir class with the name attribute set to inst_name.
print(my_instance_3.name)
This line of code prints the name attribute of the my_instance_3 and ensures that the instances were created properly.