0

How can achieve the following: I want to create unique dynamic variables in a for loop self."INSERT DYNAMIC VARIABLE HERE"

list = [box1, box2, box3]
for value in list:
 self."" = Button()
Johnn Kaita
  • 432
  • 1
  • 3
  • 13
  • 2
    Use a dictionary instead. (And don't use the built-in `list` as a name.) – Klaus D. Jan 04 '21 at 06:56
  • 1
    Yes probably use dictionary as `self.variables = dict()` and keep updating it in the loop like, `self.variables[] = value` – Chinni Jan 04 '21 at 06:58
  • Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Georgy Jan 07 '21 at 11:08

1 Answers1

1

You could use exec, but it's highly not recommended see here:

l = [box1, box2, box3]
for value in l:
    exec(f"self.{value} = Button()")

I would recommend you to just store it in a dictionary:

l = [box1, box2, box3]
self.variables = {}
for value in l:
    self.variables[value] = Button()
U13-Forward
  • 69,221
  • 14
  • 89
  • 114