getting dynamic attribute in python

Question:

I have and object with an pseudo or special attribute that can be named in three different ways (Note: I don’t control the code which generates the object)

The value in the attributes (depending which one is set) is exactly the same, and I need to get that for further processing, so depending of the source of data, I can have something like:

>>> obj.a
'value'
>>> obj.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'b'
>>> obj.c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'c'

or

>>> obj.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'a'
>>> obj.b
'value'
>>> obj.c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'c'

or

>>> obj.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'a'
>>> obj.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Obj instance has no attribute 'b'
 >>> obj.c
'value'

I’m interested in getting 'value' and unfortunately __dict__ property does not exist in that object. So what I ended doing for getting that value was just do a bunch of getattr calls. Assuming that possibilities are only three, the code looked like this:

>>> g = lambda o, l: getattr(o, l[0], getattr(o, l[1], getattr(o, l[2], None)))
>>> g(obj, ('a', 'b', 'c'))
'value'

Now, I would like to know whether there is a better way to this? As I’m 100% convinced what I’ve done 🙂

Thanks in advance

Asked By: rhormaza

||

Answers:

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
>>>
Answered By: Joran Beasley

How about:

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

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.

Answered By: kindall

There may be a better way to do this depending on the structure of your object, but knowing nothing else, here is a recursive solution that works exactly like your current solution except that it will work with an arbitrary number of arguments:

g = lambda o, l: getattr(o, l[0], g(o, l[1:])) if l else None
Answered By: Andrew Clark

And another one:

reduce(lambda x, y:x or  getattr(obj, y, None),  "a b c".split(), None)

(in Python 3 you have to import reduce from functools. It is a builtin in Python 2)

Answered By: jsbueno
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.