I want to create a dynamic object (inside another object) in Python and then add attributes to it.
I tried:
obj = someobject
obj.a = object()
setattr
Which objects are you using? Just tried that with a sample class and it worked fine:
class MyClass:
i = 123456
def f(self):
return "hello world"
b = MyClass()
b.c = MyClass()
setattr(b.c, 'test', 123)
b.c.test
And I got 123 as the answer.
The only situation where I see this failing is if you're trying a setattr on a builtin object.
Update: From the comment this is a repetition of: Why can't you add attributes to object in python?