how to pass attribute name via sys.argv
gilles.no.lenfant.spam at ingeniweb.com
Thu Jan 27 13:57:02 CET 2005
Felix Hebeler a écrit :
> Hi all,
> I am doing some Python scripting for a while, but I'm not too deep into
> it yet. So I have a problem I can't solve.
> I need to call an object attribute:
> value = object.attrName
> the problem is, that the attribute name can only be specified at runtime.
> So what I have is something like
> >>> attrName = sys.argv
> >>> attrName
> and I need to pass it on so I can call
> value = object.cellsize
> Can this be done using Python?
> Thanks for any hints
The builtin "setattr" is your friend.
"object" is now a reserved (builtin) name, use "objekt" instead.
objekt = Foo()
attrName = sys.argv
values = ['foo', 'bar', 'whatever']
setattr(objekt, attrName, values)
More information about the Python-list