This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Multiple get "itemgetter"
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: mariocj89, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2017-11-12 14:49 by mariocj89, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg306114 - (view) Author: Mario Corchero (mariocj89) * (Python triager) Date: 2017-11-12 14:49
At the moment we can get an operator that performs multiple "gets" in object attributes. Example:

>>> getter = operator.attrgetter("child1.child2")
>>> o = mock.Mock()
>>> getter(o)
<Mock name='mock.child1.child2' id='4317895480'>

On the other hand, itemgetter -which can be used for mappings- can only access single level.

  a = itemgetter("a")(d)

The proposal is to add a way to perform multiple fetches the say way attrgetter is doing it.

The main worry here for me would be that it might break some existing callers if someone had "a.b" as a key in a dict and were using itemgetter.
An option might be a new argument separator to split those so it'd look like:

  d = dict(a=dict(b=1), b=dict(c=2)) 
  ab = itemgetter("a.b", separator=".")(d)

This is effectively sugar for: itemgetter("b")(itemgetter("a")(d))

This should be available in the *args version as well so the following is valid:

  d = dict(a=dict(b=1), b=dict(c=2)) 
  ab, ac = itemgetter("a.b", "b.c", separator=".")(d)


This is coming from python-dev mailing list thread: "[Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs)"

I have a sample implementation on the py side. If this is interesting I can send a PR with the full impl (I havent started yet with the C one)
If anyone is interested in the ongoing implementation: https://github.com/mariocj89/cpython/tree/multiple_itemgetter
msg306115 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-11-12 15:32
As you have said, this will break existing code.

And this is not applicable to non-string indices.

I think that it would be easier to use a lambda or local function for this: `lambda x: x['a']['b']`. It is shorter and cleaner.

I don't see how this is related to the Python-Dev thread.
msg306140 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-11-13 05:42
I also don't think this should be done to itemgetter() which should remain focused on its core task.

Thank you for the suggestion, but am marking this as closed.  Perhaps consider posting a recipe to the ASPN cookbook (I've posted a lot of ideas there) or publish something on PyPI to see if there is any uptake.
History
Date User Action Args
2022-04-11 14:58:54adminsetgithub: 76191
2017-11-13 05:42:37rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg306140

stage: resolved
2017-11-12 15:32:06serhiy.storchakasetresolution: rejected

messages: + msg306115
nosy: + serhiy.storchaka
2017-11-12 14:49:14mariocj89create