[Python-Dev] Switch statement
Raymond Hettinger
rhettinger at ewtllc.com
Mon Jun 19 19:21:51 CEST 2006
>> A switch-statement offers only a modest readability improvement over
>> if-elif chains.
>
>
> Probably, which is why it hasn't been added yet. :-)
>
> But there is a definite readability improvement in that you *know*
> that it's always the same variable that is being compared and that no
> other conditions are snuck into some branches.
Hmm, when I saw that "arbitrary expressions" were being proposed, I took
that took mean that the variable would have to be repeated in the branches:
switch x:
case x.endswith('wart'): salicylic_acid()
case x.endswith('roid'): preparation_h()
default: chicken_soup()
> I would
> think that it would be fine if some switches could be compiled into
> some kind of lookup table while others would just be translated into a
> series of if/elifs. As long as the compiler can tell the difference.
>
That's a worthy goal; of course, the devil is in the details. Given:
switch x:
case 1: one()
case 2: two()
case 3: three()
default: too_many()
Do we require that x be hashable so that the compiler can use a lookup
table?
Raymond
More information about the Python-Dev
mailing list