16

I have an object with nested attributes like so:

obj:
    attr_1:
        attr_2

When both attr_1 and attr_2 exists, I can get it like so:

obj.attr_1.attr_2

But what if I'm not sure if either attribute exists? In this case, construction of getattr(obj, 'attr_1.attr_2', None) does not work.

What are the best practices to replace this construction?

Divide that into two getattr statements?

1

5 Answers 5

19

You can use operator.attrgetter() in order to get multiple attributes at once:

from operator import attrgetter

my_attrs = attrgetter(attr1, attr2)(obj)
Sign up to request clarification or add additional context in comments.

4 Comments

I think OP wants my_attrs = ('attr1.attr2')(obj)
The title says something different though.
@elena yes, the title makes this ambiguous.
Sorry. I want to get obj.attr1.attr2 using getattr or another method.
4

As stated in this answer, the most straightforward solution would be to use operator.attrgetter (more info in this python docs page).

If for some reason, this solution doesn't make you happy, you could use this code snippet:

def multi_getattr(obj, attr, default = None):
"""
Get a named attribute from an object; multi_getattr(x, 'a.b.c.d') is
equivalent to x.a.b.c.d. When a default argument is given, it is
returned when any attribute in the chain doesn't exist; without
it, an exception is raised when a missing attribute is encountered.

"""
attributes = attr.split(".")
for i in attributes:
    try:
        obj = getattr(obj, i)
    except AttributeError:
        if default:
            return default
        else:
            raise
return obj

# Example usage
obj  = [1,2,3]
attr = "append.__doc__.capitalize.__doc__"

multi_getattr(obj, attr) #Will return the docstring for the
                         #capitalize method of the builtin string
                         #object

from this page, which does work. I tested and used it.

Comments

3

I would suggest using python's built-in operator.attrgetter:

from operator import attrgetter
attrgetter('attr0.attr1.attr2.attr3')(obj)

Comments

0

If you have the attribute names you want to get in a list, you can do the following:

my_attrs = [getattr(obj, attr) for attr in attr_list]

Comments

0

A simple, but not very eloquent way, to get multiple attr would be to use tuples with or without brackets something like

aval, bval =  getattr(myObj,"a"), getattr(myObj,"b")

but I think you might be wanting instead to get atrribute of a contained object with the way you are using dot notation. In which case it would be something like

getattr(myObj.contained, "c")

where contained is an object cotained within myObj object and c is an attribute of contained. Let me know if this is not what you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.