[Python-ideas] Add "default" kw argument to operator.itemgetter and operator.attrgetter

Guido van Rossum guido at python.org
Sat May 5 22:13:09 EDT 2018


Hi Vincent,

Your idea is interesting but we are worried that there are not enough real
use cases where it would be useful. Have you encountered situations
yourself where this would make a difference? I am asking not for clarifying
examples (you already provided one and from that it's perfectly clear to me
what you are proposing) but for real-world code that would benefit from
this addition to the itemgetter API.

--Guido

On Wed, May 2, 2018 at 1:08 AM, Vincent Maillol <vincent.maillol at gmail.com>
wrote:

> Hi everybody,
>
> Our PEP idea would be to purpose to add a global default value for
> itemgeet and attrgetter method.
>
> This was inspired from bug 14384 (https://bugs.python.org/issue14384);
> opened by Miki TEBEKA.
>
> For example, we could do:
>
> p1 = {'x': 43; 'y': 55}
> x, y, z = itemgetter('x', 'y', 'z', default=0)(values)
> print(x, y, z)
> 43, 55, 0
>
> instead of:
>
> values = {'x': 43; 'y': 55}
> x = values.get('x', 0)
> y = values.get('y', 0)
> z = values.get('z', 0)
> print(x, y, z)
> 43, 55, 0
>
> The goal is to have have concise code and improve consistency with
> getattr, attrgetter and itemgetter
>
> What are you thinking about this?
>
> MAILLOL Vincent
> GALODE Alexandre
> _______________________________________________
> 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/
>



-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180505/6fa9f5f1/attachment.html>


More information about the Python-ideas mailing list