<div dir="ltr"><div style class="markdown-here-wrapper" id="markdown-here-wrapper-342854"><p style="margin:1.2em 0px!important">what y’all are missing about switch/case is that it’s <em>not</em> necessarily a series of comparisons in most languages.</p>
<p style="margin:1.2em 0px!important">in essence, it’s a hashmap lookup with (in some languages) optional comparisons.</p>
<p style="margin:1.2em 0px!important">in python, the most similar thing is something like this:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background:none repeat scroll 0% 0% rgb(248,248,255)" class="language-python"><span class="function"><span style="color:rgb(51,51,51);font-weight:bold" class="keyword">def</span> <span style="color:rgb(153,0,0);font-weight:bold" class="title">branch_2_3</span><span class="params">()</span>:</span>
spam(<span style="color:rgb(0,153,153)" class="number">1</span>)
spam(<span style="color:rgb(0,153,153)" class="number">6</span>)
{
<span style="color:rgb(221,17,68)" class="string">'key1'</span>: <span style="color:rgb(51,51,51);font-weight:bold" class="keyword">lambda</span>: eggs += lay_eggs(), <span style="color:rgb(153,153,136);font-style:italic" class="comment">#branch 1</span>
<span style="color:rgb(221,17,68)" class="string">'key2'</span>: branch_2_3,
<span style="color:rgb(221,17,68)" class="string">'key3'</span>: branch_2_3,
}[value]()
</code></pre>
<p style="margin:1.2em 0px!important">which is of course completely ugly and unnecessarily complicated for cases where multiple keys lead to the same branch.</p>
</div></div>