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
解决方案
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.