FW: Switch statements again

David K. Trudgett dkt at registriesltd.com.au
Thu Jan 16 01:02:58 EST 2003


On Thursday 2003-01-16 at 04:55:34 +0000, Carl Banks wrote:

> 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

"Better" is just a matter of opinion, as well as a function of the
particular programming problem involved. People have thought up
probably a dozen different ways to do a switch in Perl, some more
inspired than others.

So, there's no point elaborating for that reason (an opinion can't be
demonstrated or proved). Suffice it to say that most people who like
Python's syntax won't like the various Perl hacks to try to do a
switch statement. Perl 6, when it arrives (it's a fair way off), will
probably obviate any need to seek succour in any of those hacks.


> 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.

Yes, the perlsyn page has plenty of examples to boggle the mind. A lot
of them would be fairly unobvious to the Perl newbie. It's a matter of
opinion, though, whether the freedom to choose the best option to do
the job makes a language bad or not. I happen to like the "There's
more than one way to do it" philosophy (when not taken to absurd
lengths). I dislike Python's straightjacket, condescending approach.
Of course, Perl in its current incarnation has a lot of warts and is
far from perfect itself... Lisp, for example, far exceeds Perl in the
area of TMTOWTDI.



> Maybe you have something different in mind, though.

Not really. A hash of subroutine references comes in handy sometimes,
but then you can do that in Python, too. One of the examples on the
perlsyn page looks pretty much like a proper C switch, which isn't too
bad if you like C syntax.


David Trudgett






More information about the Python-list mailing list