-3
class Player:
    def __init__(self, x, y, z, t, f, g):
        #code
    def function1(self):
        #code    
    def function2(self):
        #code
player1 = Player(1,1,1,1,1,1)
player2 = Player(2,2,2,2,2,2)
...
player100 = Player(1,1,1,1,1,1)

while True:
    player1.function1()
    player1.function2()
    ...
    ...
    player100.function1()
    player100.function2()
    #not efficient!

Here there are 100 elements in my class. I want to call function1() and function2() for each element of class in my main loop. I searched by myself but I couldn't find it. What is the efficient way to do it? Thanks in advance.

1 Answers1

0

Create Array of 100 elements

creating list

list = []

appending instances to list


list.append(Player(1,1,1,1,1,1)) 

list.append( Player(2,2,3,4,5,6) ) 

list.append( Player(1,2,3,4,5,6) ) 

for obj in list:

     obj.function1()

     obj.function2()

this code will create list of you Player objects, 3 object and will call the function 1 and 2 3 times

Taron Qalashyan
  • 660
  • 4
  • 8