Hello, How can I achieve such job:
def get_foo(someobject, foostring):
return someobject.foostring
IE:
if I do get_foo(obj, "name")
it should be calling obj.name
(see input as string but I call it as an attritube.
Thanks
Hello, How can I achieve such job:
def get_foo(someobject, foostring):
return someobject.foostring
IE:
if I do get_foo(obj, "name")
it should be calling obj.name
(see input as string but I call it as an attritube.
Thanks
You should use setattr and getattr:
setattr(object,'property',value)
getattr(object,'property',default)
Use the builtin function getattr
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
if someobject has attribute foostring then
def get_foo(someobject, foostring):
return getattr(someobject,foostring)
or if u want to set attribute to supplied object then:
def set_foo(someobject, foostring):
return setattr(someobject,foostring, value)
Try it
Thanks