How to dynamically create variable names? Or should I simply use a dictionary?

The easies way is to have a container class, createn an empty instance and add attributes to it as required:

class MyContainer(object):
    pass

my_container = MyContainer()
my_container.first_var = 1
my_container.next_var = "Hello"
...

But there are certainly much better ways, depending on your application.
For Python3, you can/should omit the object base-class.

In case you want to create variable names dynamically (whyever that should be necessary here), you could use:

setattr(my_container, "a_string_with_the_name_of_the_attribute", "the value")

Leave a Comment