[Python-ideas] Add "default" kw argument to operator.itemgetter and operator.attrgetter
Vincent Maillol
vincent.maillol at gmail.com
Mon May 7 14:42:37 EDT 2018
Hi everybody,
> Have you encountered situations yourself where this would make a difference ?
I need to get multiple objects in several dicts using the same set of keys.
I wanted to use itemgetter to reduce the number of lines of code but I
have mixed up getattr/dict.get
that have default parameter with attrgetter/itemgetter.
> At some point, we're really better off just using a lambda.
I kept severals line with `.get('key', value)`, I find that is more readable.
> Also, I'm concerned that about increasing the complexity of itemgetter() API to serve an occasional exotic use case rather that being easy to learn and remember for the common cases.
I understand you, each additional parameter increases the cost of
maintenance and update operator module
will take a lot of work we should update c and python module, and
ensure compatibility with pickle.
I did it just to try it with itemgetter
https://github.com/Maillol/cpython/compare/master...Add-default-parameter-to-operator-itemgetter
I don't know if we add parameter `default` to itemgetter, getitem,
attrgetter the API will become
more complexe or more consistency, but I agree with you, it is an
occasional use case and we can always
use `my_dict.get`.
2018-05-07 6:07 GMT+02:00 Raymond Hettinger <raymond.hettinger at gmail.com>:
>
>> On May 6, 2018, at 6:00 AM, Steven D'Aprano <steve at pearwood.info> wrote:
>>
>> On Thu, May 03, 2018 at 04:32:09PM +1000, Steven D'Aprano wrote:
>>
>>> Maybe I'm slow today, but I'm having trouble seeing how to write this as
>>> a lambda.
>>
>> Yes, I was definitely having a "cannot brain, I have the dumb" day,
>> because it is not that hard to write using lambda. See discussion here:
>>
>> https://mail.python.org/pipermail/python-list/2018-May/732795.html
>>
>> If anything, the problem is a plethora of choices, where it isn't clear
>> which if any is the best way, or the One Obvious Way
>
> At one time, lambda was the one obvious way. Later, partial, itemgetter, attrgetter, and methodcaller were added to express common patterns for key-functions and map(). If needed, the zoo of lambda alternatives could be further extended to add a rpartial() function that partials from the right. That would have helped with Miki's example. Instead of:
>
> get = attrgetter('foo', None)
> return get(args) or get(config) or get(env)
>
> He could've written:
>
> get = rpartial(getattr, 'foo', None)
> return get(args) or get(config) or get(env)
>
> If itemgetter and attrgetter only did a single lookup, a default might make sense. However, that doesn't fit well with multiple and/or chained lookups where are number of options are possible. (See https://bugs.python.org/issue14384#msg316222 for examples and alternatives).
>
>
> Raymond
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list