John O'Connor, 22.12.2011 08:05:
On Wed, Dec 21, 2011 at 6:33 PM, Steven D'Aprano wrote:
Why create a new function for it? Why not just give attrgetter a keyword only argument default?
I need to revise my example. It should be: _x = defaultattrgetter(('x', 0)) _xy = defaultattrgetter(('x', 0), ('y', 1))
Which is what I had originally but I was too quick to change it in the course of writing thinking the dict notation looked cleaner. But, since the argument order matters a dict wont work. The same applies to using keyword arguments. I'm not sure if there is a clean way to add this type of functionally to attrgetter without strings being a special case.
I don't consider it a major use case to be able to use different default return values for different steps in the lookup process. If you want that, write your own lookup function, that's trivial enough.
If such a feature gets added (which would be for Python 3.3 or later), I second Steven's proposal of making it a keyword argument, i.e.
lookup_a_b_c = operator.attrgetter('a', 'b', 'c', default=123)