<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
I can see this being useful if I have a Django Rest Framework
validate method
(<a class="moz-txt-link-freetext" href="http://www.django-rest-framework.org/api-guide/serializers/#object-level-validation">http://www.django-rest-framework.org/api-guide/serializers/#object-level-validation</a>)
which takes a dictionary argument.<br>
<br>
def validate(self, data):<br>
{'name': name, 'address': address} = data<br>
<br>
Currently, this would look like:<br>
<br>
def validate(self, data):<br>
name, address = data['name'], data['address']<br>
<br>
It does get more useful with the extension:<br>
<br>
def validate(self, data):<br>
{'name': name, 'address': address, **rest} = data<br>
<br>
instead of:<br>
<br>
def validate(self, data):<br>
rest = data.copy()<br>
name = rest.pop('name')<br>
address = rest.pop('address')<br>
<br>
In the rest framework case, mutating data directly might not be a
problem, but this does feel like a nice syntax when avoiding
mutation is required.<br>
<br>
Regards,<br>
Ian<br>
<br>
<div class="moz-cite-prefix">On 25/05/16 14:11, Michael Selik wrote:<br>
</div>
<blockquote
cite="mid:CAGgTfkMu=FzEzoNh2VGog35Y-x-5_MFZQHdYbmJVO9xcHRB1sQ@mail.gmail.com"
type="cite">
<div dir="ltr">Python's iterable unpacking is what Lispers might
call a destructuring bind.
<div><br>
</div>
<div> py> iterable = 1, 2, 3, 4, 5</div>
<div> py> a, b, *rest = iterable</div>
<div> py> a, b, rest</div>
<div> (1, 2, (3, 4, 5))</div>
<div><br>
</div>
<div>Clojure also supports mapping destructuring. Let's add that
to Python!</div>
<div><br>
</div>
<div> py> mapping = {"a": 1, "b": 2, "c": 3}</div>
<div> py> {"a": x, "b": y, "c": z} = mapping</div>
<div> py> x, y, z</div>
<div> (1, 2, 3)</div>
<div> py> <span style="line-height:1.5">{"a": x, "b": y}
= mapping</span></div>
<div><span style="line-height:1.5"> Traceback:</span></div>
<div><span style="line-height:1.5"> ValueError: too many keys
to unpack</span></div>
<div><span style="line-height:1.5"><br>
</span></div>
<div><span style="line-height:1.5"><br>
</span></div>
<div><span style="line-height:1.5">This will be approximately as
helpful as iterable unpacking was before PEP 3132 (</span><a
moz-do-not-send="true"
href="https://www.python.org/dev/peps/pep-3132/"><a class="moz-txt-link-freetext" href="https://www.python.org/dev/peps/pep-3132/">https://www.python.org/dev/peps/pep-3132/</a></a>).</div>
<div><br>
</div>
<div>I hope to keep discussion in this thread focused on the
most basic form of dict unpacking, but we could e<span
style="line-height:1.5">xtended mapping unpacking similarly
to how PEP 3132 extended iterable unpacking. Just
brainstorming...</span></div>
<div><br>
</div>
<div> py> <span style="line-height:1.5">mapping = {"a":
1, "b": 2, "c": 3}</span></div>
<div> py> {"a": x, **rest} = mapping</div>
<div> py> x, rest</div>
<div> (1, {"b": 2, "c": 3})</div>
<div><br>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Python-ideas mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-ideas@python.org">Python-ideas@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-ideas">https://mail.python.org/mailman/listinfo/python-ideas</a>
Code of Conduct: <a class="moz-txt-link-freetext" href="http://python.org/psf/codeofconduct/">http://python.org/psf/codeofconduct/</a></pre>
</blockquote>
<br>
</body>
</html>