indirect argument assignment
jepler at unpythonic.net
jepler at unpythonic.net
Sat Oct 12 16:00:24 EDT 2002
On Sat, Oct 12, 2002 at 12:49:52PM -0700, Donnal Walter wrote:
> def Add(self, type, name, arg1, arg2):
> self.__dict__[name] = type(arg1, arg2)
> self.objectList.append(self.__dict__[name])
I'd probably write
def Add(self, constructor, name, arg1, arg2):
val = constructor(arg1, arg2)
setattr(self, name, val)
self.objectlist.append(val)
the setattr() builtin will do what you want, without naming __dict__:
Help on built-in function setattr:
setattr(...)
setattr(object, name, value)
Set a named attribute on an object; setattr(x, 'y', v) is equivalent to
``x.y = v''.
Jeff
More information about the Python-list
mailing list