Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting dynamic attribute in python [duplicate]

Tags:

python

getattr

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

like image 652
rhormaza Avatar asked Nov 28 '12 00:11

rhormaza


2 Answers

How about:

for name in 'a', 'b', 'c':     try:         thing = getattr(obj, name)     except AttributeError:         pass     else:         break 
like image 194
wim Avatar answered Sep 27 '22 19:09

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.

like image 32
kindall Avatar answered Sep 27 '22 19:09

kindall