[Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs) (original) (raw)
Mario Corchero mariocj89 at gmail.com
Sun Nov 12 09:56:23 EST 2017
- Previous message (by thread): [Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs)
- Next message (by thread): [Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Do you mean making getitems call itemgetter?
At the moment we can already do with itemgetter:
from operator import itemgetter a,b = itemgetter("a", "b")(d)
I tend to post this every time the topic comes up, but: it's highly unlikely we'll get syntax for this when we don't even have a builtin to extract multiple items from a mapping in a single operation.
You mean subitems as attrgetter does? That would be actually quite cool!
d = dict(a=dict(b=1), b=dict(c=2)) ab, ac = itemgetter("a.b", "b.c", separator=".")(d)
I've created an issue in case something like that is desired: https://bugs.python.org/issue32010 No real strong push for it, happy to just close it if it does not get interest.
That said I am not sure it solves Ben requests as he seamed to be targetting a way to bind the variable name with the dictionary keys implicitly.
On 12 November 2017 at 10:06, Nick Coghlan <ncoghlan at gmail.com> wrote:
On 11 November 2017 at 16:22, Jelle Zijlstra <jelle.zijlstra at gmail.com> wrote: > 2017-11-10 19:53 GMT-08:00 Ben Usman <bigobangux at gmail.com>: >> I was not able to find any PEPs that suggest this (search keywords: >> "PEP 445 dicts", "dictionary unpacking assignment", checked PEP-0), >> however, let me know if I am wrong. >> > It was discussed at great length on Python-ideas about a year ago. There is > a thread called "Unpacking a dict" from May 2016.
I tend to post this every time the topic comes up, but: it's highly unlikely we'll get syntax for this when we don't even have a builtin to extract multiple items from a mapping in a single operation. So if folks would like dict unpacking syntax, then a suitable place to start would be a proposal for a "getitems" builtin that allowed operations like: b, a = getitems(d, ("b", "a")) operator.itemgetter and operator.attrgetter may provide some inspiration for possible proposals. Cheers, Nick. -- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ mariocj89%40gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20171112/a4fc84a4/attachment.html>
- Previous message (by thread): [Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs)
- Next message (by thread): [Python-Dev] Analog of PEP 448 for dicts (unpacking in assignment with dict rhs)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]