On 18 April 2014 16:22, Skip Montanaro <skip@pobox.com> wrote:
On Fri, Apr 18, 2014 at 10:03 AM, Joao S. O. Bueno
<jsbueno@python.org.br> wrote:
> It may be just me, but I fail - in a complete manner - to see how this
> syntax can offer any
> improvement on current if/elif chains.

I haven't been following this thread closely, so I don't know if Lucas
has mentioned more than syntax, however...

In other languages, the semantics of the switch statement allow the
compiler to generate more efficient code. Instead of testing each
branch of the if statement in succession looking for a match, you
evaluate the switch expression once, then use it as an index into a
jump table of some sort which points directly to the matching case
label. If nothing matches, you jump to the default branch (if one was
defined) or just jump over the entire switch statement (if not).


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.

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.

(You basically note this in your follow up email - but this has been the problem with previous discussions of switch/case for Python.)

Michael

 
Skip
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html