<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On 18 April 2014 16:22, Skip Montanaro <span dir="ltr"><<a href="mailto:skip@pobox.com" target="_blank">skip@pobox.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On Fri, Apr 18, 2014 at 10:03 AM, Joao S. O. Bueno<br>
<<a href="mailto:jsbueno@python.org.br">jsbueno@python.org.br</a>> wrote:<br>
> It may be just me, but I fail - in a complete manner - to see how this<br>
> syntax can offer any<br>
> improvement on current if/elif chains.<br>
<br>
</div>I haven't been following this thread closely, so I don't know if Lucas<br>
has mentioned more than syntax, however...<br>
<br>
In other languages, the semantics of the switch statement allow the<br>
compiler to generate more efficient code. Instead of testing each<br>
branch of the if statement in succession looking for a match, you<br>
evaluate the switch expression once, then use it as an index into a<br>
jump table of some sort which points directly to the matching case<br>
label. If nothing matches, you jump to the default branch (if one was<br>
defined) or just jump over the entire switch statement (if not).<br>
<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div>In which case you compile in the switch values at "compile time", so the semantics are "surprising" (if you switch on a name instead of a fixed value then changing the object the name is bound to doesn't change the behaviour of the switch). So you either get fast but surprising (unpythonic) or dynamic and no different to an if/elif chain.</div>
<div><br></div><div>If you restrict switch to hashable literals then you can make it fast, and nicer syntactically than the standard pattern - building a dictionary of functions. But that's quite a restriction.</div><div>
<br></div><div>(You basically note this in your follow up email - but this has been the problem with previous discussions of switch/case for Python.)</div><div><br></div><div>Michael</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888">
Skip<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><pre cols="72"><a href="http://www.voidspace.org.uk/" target="_blank">http://www.voidspace.org.uk/</a><br><br>May you do good and not evil<br>May you find forgiveness for yourself and forgive others<br>
May you share freely, never taking more than you give.<br>-- the sqlite blessing <a href="http://www.sqlite.org/different.html" target="_blank">http://www.sqlite.org/different.html</a></pre>
</div></div>