5

I vaguely remember learning about some sort of built-in function that would do the equivalent of

f = lambda x: x.attr

Am I just imagining this or does such a thing exist?

2 Answers 2

11

operator.attrgetter()

Sign up to request clarification or add additional context in comments.

Comments

3
getattr(obj, 'attr')

will get the attribute attr from obj, or raise AttributeError if it doesn't exist. You can also supply a default value:

getattr(obj, 'attr', None)

in which case the default will be returned instead of raising an exception if the attribute can not be found on the object.

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.