[Python-ideas] Unpacking a dict
Rob Cliffe
rob.cliffe at btinternet.com
Thu May 26 22:34:54 EDT 2016
On 26/05/2016 18:51, Steven D'Aprano wrote:
> On Thu, May 26, 2016 at 04:10:10PM +0200, Michel Desmoulin wrote:
>
>> The thing with those details is that you can completly ignore them, and
>> don't know they exist, and simply look it up when you need it.
> What Google search terms would a Python programmer use to find out what
>
> {"1": a, "foo.bar": b, **c} = **mapping
>
> does?
>
> Please don't dismiss the effect of unfamiliar syntax on the reader.
> Adding more magic syntax increases the cost and difficulty of reading
> the code and learning the language. That cost might be justified if the
> new syntax is useful enough, but so far this syntax appears to be of
> very marginal usefulness. There's no obvious use-case where it would be
> an overwhelming benefit, at least not yet.
+1
With tuple and list unpacking
(a,b) = (1,2)
[a,b] = [1,2] # or even mixing: [a,b] = (1,2)
the structure of the LHS and RHS mirror each other, making the meaning
intuitive/obvious.
Doing the same thing for dicts:
{ 'a' : a, 'b' : b } = { 'a' : 1, 'b' : 2 }
makes the LHS too verbose to be very useful IMO. All the examples so
far can be done in other, arguably better, ways.
While a more concise syntax would break the "mirroring" and be
confusing, or at least more to learn as Steven says.
>
> I believe this syntax comes from Clojure. Can you give some examples of
> real-world code using this syntax in Clojure? (Not toy demonstrations of
> how it works, but working code that uses it to solve real problems.)
>
> If Clojure programmers don't use it, then I expect neither will Python
> programmers.
>
>
>
More information about the Python-ideas
mailing list