I want to determine which attribute to look-up dynamicaly,
Consider below python code:
def f(x, a):
return x.a
class X:
b = 4
x = X()
print(f(x, b))
If python had this property, above code should print 4.
Is there way to do it in any language?
I cannot conceive a way with C++ templates or dynamic typing.
I don’t think you can dynamically access attributes of objects in the way you are attempting. the built-in function
getattrwill allow you to do this. As marcin suggested you can just replace f withgetattrhttp://docs.python.org/library/functions.html#getattr
or