On Thu, Jul 1, 2010 at 3:56 AM, egbert <egber...@xs4all.nl> wrote: > Normally you use setattr() if the name of the attribute is in a > namestring: >>>>> setattr(self, namestring, value) > But my attributes are lists or dictionaries, and I don't seem to be > able to use setattr anymore.
Because you're not setting an attribute anymore, you're mutating an existing dict/list object (that just so happens to be the attribute of another object). > Now I use for a list something like: >>>> self.__dict__[namestring].append(value) getattr(self, namestring).append(value) > and for a dictionary: >>>> self.__dict__[namestring][keystring]=value getattr(self, namestring)[keystring] = value > But I have the impression that I am cheating, because users should not > operate on __dict__ directly. > Is that true ? And are there better solutions ? Yes and yes. See above. When dealing with nested stuff (e.g x.y[z]), switch to getattr() as demonstrated above. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list