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?
foo = ???is creating the instance? (e.g.foo = Foo()). And the value (42), where does that come from? How come it is not also passed as an argument tocreateFoo? Isn't this question a duplicate of stackoverflow.com/questions/285061/… ?