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() 
3
  • 2
    Use a dictionary instead. (And don't use the built-in list as a name.) Commented Jan 4, 2021 at 6:56
  • 1
    Yes probably use dictionary as self.variables = dict() and keep updating it in the loop like, self.variables[<name>] = value Commented Jan 4, 2021 at 6:58
  • Does this answer your question? How do I create variable variables? Commented Jan 7, 2021 at 11:08

1 Answer 1

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() 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.