Hey, I just started wondering about this as I came upon a code that expected an object with a certain set of attributes (but with no specification of what type this object should be).
One solution would be to create a new class that has the attributes the code expects, but as I call other code that also needs objects with (other) attributes, I'd have to create more and more classes.
A shorter solution is to create a generic class, and then set the attributes on instances of it (for those who thought of using an instance of
def __init__(self, **kw):
for name in kw:
setattr(self, name, kw[name])
options = data(do_good_stuff=True, do_bad_stuff=False)
The original code can be streamlined a little by using
In : class data: ...: def __init__(self, **kwargs): ...: self.__dict__.update(kwargs) ...: In : d = data(foo=1, bar=2) In : d.foo Out: 1 In : d.bar Out: 2