(original) (raw)
2017-11-10 19:53 GMT-08:00 Ben Usman <bigobangux@gmail.com>:
The following works now:seq = \[1, 2\]d = {'c': 3, 'a': 1, 'b': 2}(el1, el2) = \*seqel1, el2 = \*seqhead, \*tail = \*seqseq\_new = (\*seq, \*tail)dict\_new = {\*\*d, \*\*{'c': 4}}def f(arg1, arg2, a, b, c):passf(\*seq, \*\*d)It seems like dict unpacking syntax would not be fully coherent withlist unpacking syntax without something like:{b, a, \*\*other} = \*\*dBecause iterables have both syntax for function call unpacking and"rhs in assignment unpacking" and dict has only function callunpacking syntax.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.
The main use-case, in my understating, is getting shortcuts toelements of a dictionary if they are going to be used more thenones later in the scope. A made-up example is using a config toinitiate a bunch of things with many config arguments with longnames that have overlap in keywords used in initialization.One should either write long calls likestart\_a(config\['parameter1'\], config\['parameter2'\],config\['parameter3'\], config\['parameter4'\])start\_b(config\['parameter3'\], config\['parameter2'\],config\['parameter3'\], config\['parameter4'\])many times or use a list-comprehension solution mentioned above.It becomes even worse (in terms of readability) with nested structures.start\_b(config\['group2'\]\['parameter3'\], config\['parameter2'\], config\['parameter3'\], config\['group2'\]\['parameter3'\]) ## RationaleRight now this problem is often solved using \[list\] comprehensions,but this is somewhat verbose:a, b = (d\[k\] for k in \['a', 'b'\])or direct per-instance assignment (looks simple for withsingle-character keys, but often becomes very verbose withreal-world long key names)a = d\['a'\]b = d\['b'\]Alternatively one could have a very basic method\\functionget\_n() or \_\_getitem\_\_() accepting more then a single argumenta, b = d.get\_n('a', 'b')a, b = get\_n(d, 'a', 'b')a, b = d\['a', 'b'\]All these approaches require verbose double-mentioning of samekey. It becomes even worse if you have nested structuresof dictionaries.## Concerns and questions:0\. This is the most troubling part, imho, other questionsare more like common thoughts. It seems (to put it mildly)weird that execution flow depends on names of local variables.For example, one can not easily refactor these variable names. However,same is true for dictionary keys anyway: you can not suddenly decideand refactor your code to expect dictionaries with keys 'c' and'd' whereas your entire system still expects you to use dictionarieswith keys 'a' and 'b'. A counter-objection is that this specificscenario is usually handled with record\\struct-like classes withfixed members rather then dicts, so this is not an issue.Quite a few languages (closure and javascript to name a few) seemto have this feature now and it seems like they did not suffer toomuch from refactoring hell. This does not mean that their approachis good, just that it is "manageable".1\. This line seems coherent with sequence syntax, but redundant:{b, a, \*\*other} = \*\*dand the following use of "throwaway" variable just looks poor visually{b, a, \*\*\_} = \*\*dcould it be less verbose like this{b, a} = \*\*dbut it is not very coherent with lists behavior.E.g. what if that line did not raise something like "ValueError:Too many keys to unpack, got an unexpected keyword argument 'c'".2\. Unpacking in other contexts{self.a, b, \*\*other} = \*\*dshould it be interpreted asself.a, b = d\['a'\], d\['b'\]orself.a, b = d\['self.a'\], d\['b'\]probably the first, but what I am saying is that these name-extractingrules should be strictly specified and it might not be trivial.---Ben
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/ jelle.zijlstra%40gmail.com