[Python-ideas] Yet Another Switch-Case Syntax Proposal

Skip Montanaro skip at pobox.com
Fri Apr 18 17:22:14 CEST 2014


On Fri, Apr 18, 2014 at 10:03 AM, Joao S. O. Bueno
<jsbueno at 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).

Skip


More information about the Python-ideas mailing list