FW: Switch statements again

Carl Banks imbosol at vt.edu
Thu Jan 16 05:55:34 CET 2003

David K. Trudgett wrote:
> On Wednesday 2003-01-15 at 18:21:40 -0500, Steven Scott wrote:
>> Quoting Steven Scott <Steven.Scott at Synchrologic.com>:
>> > 
>> > If/elif/else remains the most common method.  If Python ever gains
>> > something
>> > like a switch statement you can bet the farm it won't have a "fall thru"
>> > feature though, so the way you've coded your C switch statement wouldn't
>> > work.
>> > 
>> but the absolute /only/ reason you'd use a switch over an if (besides
>> looking better) is fall through.
> Pascal has no fall though on its case statement, but presumably Wirth
> thought it was still worth it. A hundred elifs in a row is just plain
> ugly, anyway. Python should get a case/switch statement, just as Perl
> 6 is going to get one. Leaving it out of both languages to begin with
> was misguided. (At least Perl has always had better ways around the
> problem than Python.)

I'm curious what are the better ways Perl has around this problem--can
you elaborate?  I'm curious because I often quote some of Perl's
convoluted examples of switch statements, such as the ones listed on
the perlsyn man page, as evidence of why Perl is such a bad language.
Maybe you have something different in mind, though.

Personally, the elifs don't bother me in the least.  In Python, the
switches are likely to make indentation a pain; it's not clear whether
case tags should be indented or not.  If they are, you have two
indentation levels for the code inside.  If they aren't, you've broken
a fundamental property of indentation.

I think Python would to better to optimize if..elif..else chains,
converting them into a branched goto (which, IIRC, Perl already does),
than to add a new syntax for it.


More information about the Python-list mailing list