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?
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.