<div dir="ltr"><div><div>I'm responding here to Sven, Random832, and Ethan.</div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr">On Wed, May 25, 2016 at 10:08 AM Sven R. Kunze <<a href="mailto:srkunze@mail.de">srkunze@mail.de</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div bgcolor="#FFFFFF">I for one find this one of the shortest proposals compared to other recent proposals I have seen. :)</div></blockquote><div><br></div><div>If it's easy to explain, it might be a good idea :-)</div></div><br><br><br><div class="gmail_quote"><div dir="ltr">On Wed, May 25, 2016 at 10:40 AM Random832 <<a href="mailto:random832@fastmail.com">random832@fastmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Wed, May 25, 2016, at 09:11, Michael Selik wrote:<br>> Clojure also supports mapping destructuring. Let's add that to Python!<br>
><br>
> py> mapping = {"a": 1, "b": 2, "c": 3}<br>
> py> {"a": x, "b": y, "c": z} = mapping<br><br>
How is this better than:<br>py> mapping = {"a": 1, "b": 2, "c": 3}<br>py> x, y, z = mapping[k] for k in ("a", "b", "c")<br></blockquote><div><span style="line-height:1.5"><br></span></div><div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif">I think the thread has formed a consensus that there are at least 2 clear use cases for unpacking. Not surprisingly, they're the same use cases for both tuple unpacking and dict unpacking.</div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif">1. declarative schema validation while simultaneously binding variables</div><div>2. declaratively extracting a subset of the elements</div><br class="inbox-inbox-Apple-interchange-newline"></div><div><span style="line-height:1.5">In your example, what if the dict has more keys than you are looping over?</span><br></div><div>Look at the other part of my proposal:</div><div><br></div><div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"> py> mapping = {"a": 1, "b": 2, "c": 3}</div></div><div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"> py><span class="inbox-inbox-Apple-converted-space"> </span><span style="line-height:1.5">{"a": x, "b": y} = mapping</span></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5"> Traceback:</span></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5"> ValueError: too many keys to unpack</span></div></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"><br></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"><span style="line-height:1.5">I really like Sven's example.</span></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"><br></div><div style="color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif;font-size:13px"> <span style="line-height:1.5">py> mapping = {"a": 1, "b": 2, "c": 3}</span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"> py><span class="inbox-inbox-inbox-inbox-Apple-converted-space"> </span><span style="line-height:1.5">{"a": x, "b": y, "c": 2} = mapping</span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><span style="line-height:1.5"> Traceback:</span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><span style="line-height:1.5"> ValueError: key 'c' does not match value 2</span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><span style="line-height:1.5"><br></span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><span style="line-height:1.5">Even if we don't implement this feature in the first version of dict unpacking, we should keep the option open.</span></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><br></div><div style="font-size:13px;color:rgb(33,33,33);font-family:'helvetica neue',helvetica,arial,sans-serif"><br></div><div><br></div><div><span style="line-height:1.5">On Wed, May 25, 2016 at 4:14 PM Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:</span><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">The proposal is this:<br> a, b = **mapping<br><br>The advantages:<br>- much more readable<br>- less duplication<br></blockquote><div><br></div><div>Why doesn't that work for tuple unpacking?</div><div><div><div><div><span style="line-height:1.5"> py> a, b = *iterable</span></div></div></div><div><span style="line-height:1.5"> SyntaxError</span></div><div><br></div><div>Whatever the reasons, that syntax wasn't chosen for tuple unpacking. Dict unpacking should mimic tuple unpacking. If I saw ``<span style="line-height:1.5">a, b = **mapping`` I would expect ``</span><span style="line-height:1.5">a, b = *iterable``.</span></div></div><div><div><br></div></div></div><div>Unpacking a tuple mirrors a tuple display.<br></div><div><br></div><div> py> (a, b) = (1, 2)</div><div> py> (a, b) = (1, 2, 3)</div><div> ValueError: too many values to unpack, expected 2</div><div><br></div><div>Unpacking a dict should mirror a dict display.</div><div><br></div><div> py> {'x': a, 'y': b} = {'x': 1, 'y': 2}</div><div> <span style="line-height:1.5">py> {'x': a, 'y': b} = {'x': 1, 'y': 2, 'z': 3}</span></div><div><span style="line-height:1.5"> ValueError: too many keys to unpack, expected {'x', 'y'}</span></div><div><br></div><div><span style="line-height:1.5">As Brendan and others have mentioned, the more concise syntax you're proposing will not support non-string keys and cannot be enhanced to support Erlang/Clojure/etc-style matching on values.</span></div><div><div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr"><br class="inbox-inbox-Apple-interchange-newline">On Wed, May 25, 2016 at 11:18 AM Paul Moore <<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">get a set of elements and *ignore* the rest:</blockquote></div><div><br></div><div>If it's just one or two, that's easy. Use an underscore to indicate you don't care. Again, I'm trying to mirror a dict display. This is the same way that tuple unpacking solves the problem.</div><div><br></div><div> py> (a, b, _) = (1, 2, 3)</div><div><div><span style="line-height:1.5"> </span><span style="line-height:1.5">py> {'x': a, 'y': b, 'z': _} = {'x': 1, 'y': 2, 'z': 3}</span></div></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">If you need to ignore many, we need to extend dict unpacking the same way that tuple unpacking was extended in PEP 3132.</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5"> py> (a, b, *rest) = (1, 2, 3, 4)</span></div><div><span style="line-height:1.5"> py> a, b, rest</span></div><div><span style="line-height:1.5"> (1, 2, (3, 4))</span></div><div><span style="line-height:1.5"> py> {'x': a, 'y', b, **rest} = </span><span style="line-height:1.5">{'x': 1, 'y': 2, 'z': 3, 'w': 4}</span></div><div> py> a, b, rest</div><div> (1, 2, {'w': 4, 'z': 3})</div><div><br></div><div>Sure, **rest isn't valid in a dict display, but it's the same with tuple unpacking: *rest isn't valid in a tuple display.</div></div></div>