<div dir="ltr">I'm sorry that I didn't send a copy of the discussions here.<div><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Thautwarm Zhao</b> <span dir="ltr"><<a href="mailto:yaoxiansamma@gmail.com" target="_blank">yaoxiansamma@gmail.com</a>></span><br>Date: 2018-04-09 1:24 GMT+08:00<br>Subject: Re: [Python-ideas] Is there any idea about dictionary destructing?<br>To: "Eric V. Smith" <<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>><br><br><br><div dir="ltr">Thank you, Eric. Your links really help me and I've investigated it carefully. <div><br><div>After reading them, I found the discussion almost focused on a non-nested data structure.</div><div><br><div><div>The flatten key-value pairs might be easily replaced by something like</div><div><br></div><div>      x, y, z = [some_dict[k] for k in ('a', 'b', 'c')]</div><div><br></div><div>I couldn't agree more but, when it comes to nested,</div><div><br></div><div>     some_dict = {</div><div>              'a': {</div><div>                   'b': {</div><div>                         'c': V1}, </div><div>                   'e': V2</div><div>                    },</div><div>              'f': V3</div><div>      }</div><div><br></div><div>I agree that there could be other ways <span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">as intuitive as the dict destructing</span> to </div><div>get `V1, V2, V3` instead, however dict destructing refers to the consistency of Python language behaviours.</div><div><br></div><div>When I'm writing these codes:</div><div>   </div><div>    [a, *b] = [1, 2, 3]</div><div><br></div><div>The LHS is actually equals to RHS, and if we implement a way to apply this on dictionary</div><div>    </div><div>    {'a': a, 'b': b, '@': c, **other} = {'a': 1, 'b': 2, '@': 3, '*': 4}</div><div><br></div><div>It also presents that LHS equals to RHS. Dict destructing/constructing is totally compatible to Python unpack/pack, </div><div>just as what iterable destructing/constructing does. It's neat when we talk about Python's data structures we can talk about </div><div>the consistency, readability and the expression of intuition.</div><div><br></div><div>In the real world, the following one could really help when it comes to the field of data storage. </div><div>    </div><div>     some_dict = 

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">{'a': [1, 2, 3, {"d": 4, "f": 5}]}</span>

</div><div>     {'a': [b, *c, {"d": e, **_}]} = 

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">some_dict<span> </span></span>

</div><div><br></div><div>The LHS doesn't only show the structure of some variable intuitively(this makes review easier, too), but also supplies a way to access data in fewer codes.<br></div><div><br></div><div>In the previous talk people have shown multiple usages of dict destructing in real world:</div><div><br></div><div>       - Django Rest Framework validate </div><div>       - Load config files and use them,  specifically Yaml/JSON data access.</div><div><br></div><div>In fact, any other operation on 

<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">dictionary<span> </span></span>than simply getting a value from a key might needs
<span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:small;font-style:normal;font-variant-ligatures:normal;font-variant-caps:normal;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">dict destructing, just when the task is complicated enough.</span></div><div><br></div><div>I do think the usages are general enough now to make us allow similar syntax to do above tasks.</div><div> </div><div>P.S:</div><div><br></div><div>Some other advices in the previous talk like the following:</div><div><br></div><div>     'a' as x, 'b' as y, 'c' as z = some_dict</div><div>     'a': x, 'b': y, 'c': z = some_dict</div><div>     mode, height, width = **prefs</div><div><br></div><div>Either of them conflicts against the current syntax, or does mismatch the consistency of Python language(LHS != RHS).</div><span class="m_8077650121238271046HOEnZb"><font color="#888888"><div><br></div><div>thautwarm</div></font></span><div><div class="m_8077650121238271046h5"><div><br></div><div><div class="gmail_extra"><br><div class="gmail_quote">2018-04-08 5:39 GMT+08:00 Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">There was a long thread last year on a subject, titled "Dictionary destructing and unpacking.":<br>
<a href="https://mail.python.org/pipermail/python-ideas/2017-June/045963.html" rel="noreferrer" target="_blank">https://mail.python.org/piperm<wbr>ail/python-ideas/2017-June/045<wbr>963.html</a><br>
<br>
You might want to read through it and see what ideas and problems were raised then.<br>
<br>
In that discussion, there's also a link to an older pattern matching thread:<br>
<a href="https://mail.python.org/pipermail/python-ideas/2015-April/032907.html" rel="noreferrer" target="_blank">https://mail.python.org/piperm<wbr>ail/python-ideas/2015-April/03<wbr>2907.html</a><br>
<br>
Eric<span><br>
<br>
On 4/7/2018 1:26 PM, thautwarm wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span>
We know that Python support the destructing of iterable objects.<br>
<br>
m_iter= (_for _in range(10))<br></span>
a,*b, c= m_iter<span><br>
<br>
That's pretty cool! It's really convenient when there're many corner cases to handle with iterable collections.<br>
However destructing in Python could be more convenient if we support dictionary destructing.<br>
<br>
In my opinion, dictionary destructing is not difficult to implement and makes the syntax more expressive. A typical example is data access on nested data structures(just like JSON), destructing a dictionary makes the logic quite clear:<br>
<br>
data= {<br>
     "direct": "some data",<br>
     "nested": {<br></span>
         "lst_data": [1,2,3],<span><br>
         "int_data": 1<br>
}<br>
}<br>
{<br>
    "direct": direct,<br>
     "nested": {<br>
         "lst_data": [a, b, c],<br>
     }<br>
}= data<br>
<br>
<br>
Dictionary destructing might not be very well-known but it really helps. The operations on nested key-value collections are very frequent, and the codes for business logic are not readable enough until now. Moreover Python is now popular in data processing which must be enhanced by the entire support of data destructing.<br>
<br>
Here are some implementations of other languages:<br>
Elixir, which is also a popular dynamic language nowadays.<br>
<br>
|iex> %{} = %{:a => 1, 2 => :b} %{2 => :b, :a => 1} iex> %{:a => a} = %{:a => 1, 2 => :b} %{2 => :b, :a => 1} iex> a 1 iex> %{:c => c} = %{:a => 1, 2 => :b} ** (MatchError) no match of right hand side value: %{2 => :b, :a => 1}|<br>
<br>
And in F#, there is something similar to dictionary destructing(actually, this destructs `struct` instead)<br></span>
type MyRecord = { Name: string; ID: int } letIsMatchByName record1 (name: string) = matchrecord1 with| { MyRecord.Name = nameFound; MyRecord.ID = _; } whennameFound = name -> true| _ -> falseletrecordX = { Name = "Parker"; ID = 10} letisMatched1 = IsMatchByName recordX "Parker"letisMatched2 = IsMatchByName recordX "Hartono"<span><br>
<br>
All of them partially destructs(or matches) a dictionary.<br>
<br>
thautwarm<br>
<br>
<br>
<br></span><span>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
<br>
</span></blockquote>
</blockquote></div><br></div></div></div></div></div></div></div></div>
</div><br></div></div>