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

Vincent Maillol vincent.maillol at gmail.com
Wed May 2 04:08:55 EDT 2018


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


More information about the Python-ideas mailing list