Categories
getattr python python-3.x

python getattr() with multiple params

Construction getattr(obj, 'attr1.attr2', None) does not work.
What are the best practices to replace this construction?
Divide that into two getattr statements?

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

from operator import attrgetter
my_attrs = attrgetter(attr1, attr2)(obj)