Dynamic name to function

166 Views Asked by At

What i want to do is to make a procedural variable name for a pygame draw function inside a for loop. But i just cant figure out how to do it.

I tried to follow some guides that i saw about dynamic names but they only showcased making a variable name for ints and strings.

I want to give all of rectangles their own name with a number at the end to show in what loop they were created. But i do not know how change the name of the variable from one loop to the other

This is the variable i need to name: pygame.draw.rect(screen, self.color, (self.pos[0] + self.scale * i , (self.pos[1] + (self.scale * x)), self.scale, self.scale))

Example: rect_(procedural number) = pygame.draw.rect(screen, self.color, (self.pos[0] + self.scale * i , (self.pos[1] + (self.scale * x)), self.scale, self.scale))

def invRect(self):
  tset = 0
  for x in range(self.rows):
    for i in range(self.columns):
      tset += 1
      this is the function i want to give a dynamic name ----->  pygame.draw.rect(screen, self.color, (self.pos[0] + self.scale * i , (self.pos[1] + (self.scale * x)), self.scale, self.scale))
      
1

There are 1 best solutions below

1
JohnyCapo On

You can make a dict which stores pointer to a function (if you do not add parentheses the variable acts as a function). If you add parentheses it only stores the result of the function -> return value.

funcs = dict({})

funcs[variable_name] = pygame.draw.rect

afterwards you can call it as

funcs[variable_name](parameters)