Allowing missing item or attributes in operator's itemgetter and attrgetter

As was previously discussed in Ability to specify default values on itemgetter and attrgetter, I think it’s important to address how default values are specified when multiple items are to be returned.

It can be one default value for all items:

itemgetter('a', 'b', default=0)({}) # returns (0, 0)

Or a mapping of default values:

itemgetter('a', 'b', default={'a': 1, 'b': 0})({}) # returns (1, 0)

Or a tuple of default values:

itemgetter('a', 'b', default=(1, 0))({}) # returns (1, 0)

I’d love to have this feature too, but it should probably be discussed in a separate thread.

2 Likes