Discussion:
[issue14384] Add "default" kw argument to operator.itemgetter and operator.attrgetter
Miki Tebeka
2012-03-22 01:09:19 UTC
Permalink
New submission from Miki Tebeka <miki.tebeka at gmail.com>:

This way they will behave more like getattr and the dictionary get.

If default is not specified, then if the item/attr not found, an execption will be raised, which is the current behavior.

However if default is specified, then return it in case when item/attr not found - default value will be returned.

I wanted this when trying to get configuration from a list of objects. I'd like to do
get = attrgetter('foo', None)
return get(args) or get(config) or get(env)

----------
components: Library (Lib)
messages: 156531
nosy: tebeka
priority: normal
severity: normal
status: open
title: Add "default" kw argument to operator.itemgetter and operator.attrgetter
versions: Python 3.3, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________
R. David Murray
2012-03-22 01:35:33 UTC
Permalink
R. David Murray <rdmurray at bitdance.com> added the comment:

Thanks for the suggestion.

It is an interesting idea, but there are some issues. attrgetter and itemgetter can take more than one key. It would probably make more sense to have the returned function be the one that takes the default, but in that case you might as well just use getattr. If we did accept a keyword-only argument for a default, what does the default mean if there is more than one key? And what if there are dotted names in the attrgetter keys?

I'm inclined to reject this as too complex for a marginal a use case. But perhaps a python-ideas discussion would be appropriate.

----------
nosy: +r.david.murray
type: -> enhancement
versions: -Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________
Miki Tebeka
2012-03-22 16:46:27 UTC
Permalink
Miki Tebeka <miki.tebeka at gmail.com> added the comment:

IMO in the case of multiple items/attrs then you return default in their place:
attrgetter('x', 'y', default=7)(None) => (7, 7)

In case of dotted attribute again it'll return default value of any of the attributes is not found:
attrgetter('x.y', default=7)(None) => 7

BTW: This is inspired from Clojure's get-in (http://bit.ly/GGzqjh) function.

I'll bring this up in python-ideas.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________
Miki Tebeka
2012-03-22 16:50:25 UTC
Permalink
Miki Tebeka <miki.tebeka at gmail.com> added the comment:

python-ideas post at https://groups.google.com/d/msg/python-ideas/lc_hkpKNvAg/ledftgY0mFUJ

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________
R. David Murray
2012-03-22 16:57:27 UTC
Permalink
R. David Murray <rdmurray at bitdance.com> added the comment:

Great. I'm going to close the issue, but it's only "for now": if you get a good response and a design agreement on python-ideas please reopen the issue. Of course, we'd need a patch, too; and I believe there would need to be close to zero impact on performance, since as I understand it a large part of the point of attrgetter and friends is speed.

----------
nosy: +rhettinger
resolution: -> later
stage: -> committed/rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________
Raymond Hettinger
2012-03-22 19:12:55 UTC
Permalink
Changes by Raymond Hettinger <raymond.hettinger at gmail.com>:


----------
assignee: -> rhettinger

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14384>
_______________________________________

Loading...