I want to create an object (say foo
) with dynamically created attributes. I can do something like this:
class Foo:
pass
foo.bar = 42
...
print(foo.bar) # 42
However, when I create the attribute, I don't know the name of the attribute yet. What I need is something like this:
def createFoo(attributeName):
foo = ???
??? = 42
...
foo = createFoo("bar")
print(foo.bar) # 42
How can I achieve this?