[Python-Dev] Switch statement

BJörn Lindqvist bjourne at gmail.com
Mon Jun 26 03:10:22 CEST 2006

On 6/23/06, Edward C. Jones <edcjones at comcast.net> wrote:
> Python is a beautiful simple language with a rich standard library.
> Python has done fine without a switch statement up to now. Guido left it
> out of the original language for some reason (my guess is simplicity).
> Why is it needed now? What would be added next: do while or goto? The
> urge to add syntax should be resisted unless there is a high payoff
> (such as yield).
> There are much better ways for the developers to spend their time and
> energy (refactoring os comes to mind).
> Please keep Python simple.
> -1 on the switch statement.

I agree. IMHO switch is a useless statement which can cause many
problems in any language. It misleads programmers to dispatch in the
wrong way. If you have switch with < 5 cases, an if-elif chain fits
just fine. If the switch is larger use a dictionary that maps values
to functions. In C, many times a switch block starts small (40 lines)
but grows as the number of values to dispatch on increases. Soon it
becomes a 500 line monstrosity that is impossible to refactor because
variables from the enclosing space is used frivolously.

I don't get the speed argument either. Who cares that if-elif-chains
are O(n) and switch O(1)? If n > 10 you are doing something wrong
anyway. I don't think I have ever seen in any language a switch
construct that, barring speed concerns, wouldn't be better written
using any other dispatch mechanism than switch.

mvh Björn

More information about the Python-Dev mailing list