getting dynamic attribute in python [duplicate] getting dynamic attribute in python [duplicate] python python

getting dynamic attribute in python [duplicate]


How about:

for name in 'a', 'b', 'c':    try:        thing = getattr(obj, name)    except AttributeError:        pass    else:        break


This has the advantage of working with any number of items:

 def getfirstattr(obj, *attrs):     return next((getattr(obj, attr) for attr in attrs                   if hasattr(obj, attr)), None)

This does have the very minor drawback that it does two lookups on the final value: once to check that the attribute exists, another to actually get the value. This can be avoided by using a nested generator expression:

 def getfirstattr(obj, *attrs):     return next((val for val in (getattr(obj, attr, None) for attr in attrs)                  if val is not None), None)

But I don't really feel it's a big deal. The generator expression is probably going to be faster than a plain old loop even with the double-lookup.


I think using dir will get u essentially the same thing __dict__ normally does ...

targetValue = "value"for k in dir(obj):    if getattr(obj,k) == targetValue:       print "%s=%s"%(k,targetValue)

something like

>>> class x:...    a = "value"...>>> dir(x)['__doc__', '__module__', 'a']>>> X = x()>>> dir(X)['__doc__', '__module__', 'a']>>> for k in dir(X):...     if getattr(X,k) == "value":...        print "%s=%s"%(k,getattr(X,k))...a=value>>>