<div dir="ltr"><div>And this should print:</div><div><br></div>'some data'<div>1</div><div>2</div><div>3</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Apr 7, 2018 at 4:16 PM, Nikolas Vanderhoof <span dir="ltr"><<a href="mailto:nikolasrvanderhoof@gmail.com" target="_blank">nikolasrvanderhoof@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>This would be a very handy feature, but Coconut (which is just python with some extra functional-style features) also has support for this kind of pattern-matching:</div><div><a href="http://coconut-lang.org" target="_blank">http://coconut-lang.org</a><br></div><div><br></div><div><img src="cid:ii_jfpt3gm40_162a1b4dd6fac1cd" width="562" height="64"><br>​Since Coconut will compile to Python (2 or 3) you can just write in Coconut and use the resulting code in your Python.<br></div><div><br></div><div>Using your first example in coconut would be nearly identical, except I believe the entire dictionary must be specified (I am not sure about this).</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><span class=""><div>data = {</div><div>    'direct': 'some data',</div><div>    'nested': {</div><div>        'lst_data': [1, 2, 3],</div><div>        'int_data': 1</div><div>    }</div><div>}</div><div><br></div><div>{</div><div>    'direct': direct,</div><div>    'nested': {</div><div>        'lst_data': [a, b, c],</div></span><div>        'int_data': _</div><div>    }</div><div>} = data</div><div><br></div><div>print(direct)</div><div>print(a)</div><div>print(b)</div><div>print(c)</div></blockquote><div><br></div><div>And this should print:</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Sat, Apr 7, 2018 at 1:26 PM, thautwarm <span dir="ltr"><<a href="mailto:yaoxiansamma@gmail.com" target="_blank">yaoxiansamma@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div id="m_-2752385003177931860m_7696254533562765893__MailbirdStyleContent" style="font-size:10pt;font-family:arial;color:#000000"><div><span style="font-size:10pt;line-height:1.5">We know that Python support the destructing of iterable objects.</span></div><div><pre style="font-family:consolas;font-size:7.2pt;background-color:rgb(248,248,255)">m_iter <span style="font-weight:bold">= </span>(_ <span style="color:#475cd4;font-weight:bold">for </span>_ <span style="color:#475cd4;font-weight:bold">in </span><span style="color:#0086b3">range</span>(<span style="color:#009999">10</span>))<br>a, <span style="font-weight:bold">*</span>b, c <span style="font-weight:bold">= </span>m_iter</pre></div><div><span style="font-size:10pt;line-height:1.5">That's pretty cool! It's really convenient when there're many corner cases to handle with iterable collections.</span></div><div>However destructing in Python could be more convenient if we support dictionary destructing.</div><div><span style="font-size:10pt;line-height:1.5"><br></span></div><div><span style="font-size:10pt;line-height:1.5">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), </span><span style="font-size:13.3333px">destructing</span><span style="font-size:10pt;line-height:1.5"> a dictionary makes the logic quite clear:</span></div><div><pre style="font-family:consolas;font-size:7.2pt;background-color:rgb(248,248,255)"><pre style="font-family:consolas;font-size:7.2pt">data <span style="font-weight:bold">= </span>{<br>    <span style="color:#008080;font-weight:bold">"direct"</span><span style="font-weight:bold">: </span><span style="color:#008080;font-weight:bold">"some data"</span>,<br>    <span style="color:#008080;font-weight:bold">"nested"</span><span style="font-weight:bold">: </span>{<br>        <span style="color:#008080;font-weight:bold">"lst_data"</span><span style="font-weight:bold">: </span>[<span style="color:#009999">1</span>, <span style="color:#009999">2</span>, <span style="color:#009999">3</span>],<br>        <span style="color:#008080;font-weight:bold">"int_data"</span><span style="font-weight:bold">: </span><span style="color:#009999">1<br></span><span style="color:#009999">    </span>}<br>}<br>{<br>   <span style="color:#008080;font-weight:bold">"direct"</span><span style="font-weight:bold">: </span>direct,<br>    <span style="color:#008080;font-weight:bold">"nested"</span><span style="font-weight:bold">: </span>{<br>        <span style="color:#008080;font-weight:bold">"lst_data"</span><span style="font-weight:bold">: </span>[a, b, c],<br>    }<br>} <span style="font-weight:bold">= </span>data </pre></pre></div><div><br></div><div><span style="font-size:10pt;line-height:1.5">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.</span></div><div><br></div><div>Here are some implementations of other languages:<br><div><div>Elixir, which is also a popular dynamic language nowadays.</div><div><pre style="font-family:monaco,monospace,courier,"courier new";font-size:0.846154em;overflow:auto;line-height:1.69231em;margin-bottom:1.69231em;padding:15px 20px;word-wrap:break-word;border:1px solid rgb(246,228,204);color:rgb(85,85,85);font-variant-ligatures:normal;background:rgb(255,248,237)"><code class="m_-2752385003177931860m_7696254533562765893language-iex" style="font-family:monaco,monospace,courier,"courier new";font-size:1em;padding:0px;border:0px;background:transparent">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}</code></pre></div><div>And in F#, there is something similar to dictionary destructing(actually, this destructs `struct` instead)</div><div><span class="m_-2752385003177931860m_7696254533562765893hljs-class" style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap"><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253)">type</span> <span class="m_-2752385003177931860m_7696254533562765893hljs-title" style="color:rgb(0,125,154)">MyRecord</span> </span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">= { Name: string; ID: int }

</span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">let</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> IsMatchByName record1 (name: string) =
    </span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">match</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> record1 </span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">with</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">
    | { MyRecord.Name = nameFound; MyRecord.ID = _; } </span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">when</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> nameFound = name -> </span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">true</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">
    | _ -> </span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">false</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">

</span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">let</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> recordX = { Name = </span><span class="m_-2752385003177931860m_7696254533562765893hljs-string" style="color:rgb(163,21,21);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">"Parker"</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">; ID = </span><span class="m_-2752385003177931860m_7696254533562765893hljs-number" style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">10</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> }
</span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">let</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> isMatched1 = IsMatchByName recordX </span><span class="m_-2752385003177931860m_7696254533562765893hljs-string" style="color:rgb(163,21,21);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">"Parker"</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)">
</span><span class="m_-2752385003177931860m_7696254533562765893hljs-keyword" style="color:rgb(1,1,253);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">let</span><span style="font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap;background-color:rgb(249,249,249)"> isMatched2 = IsMatchByName recordX </span><span class="m_-2752385003177931860m_7696254533562765893hljs-string" style="color:rgb(163,21,21);font-family:consolas,menlo,monaco,"lucida console","liberation mono","dejavu sans mono","bitstream vera sans mono","courier new",monospace,sans-serif;font-size:14px;font-variant-ligatures:normal;white-space:pre-wrap">"Hartono"</span></div><div><span style="font-size:10pt;line-height:1.5"><br></span></div><div>All of them partially destructs(or matches) a dictionary.</div><span class="m_-2752385003177931860HOEnZb"><font color="#888888"><div><br></div><div>thautwarm</div><div><span style="font-family:monaco,monospace,courier,"courier new";font-size:1em;color:rgb(85,85,85);line-height:1.69231em;white-space:pre-wrap;background-color:transparent"><br></span></div></font></span></div></div></div><br></div></div>______________________________<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></blockquote></div><br></div>
</blockquote></div><br></div>