FW: Switch statements again

Dan Bishop danb_83 at yahoo.com
Thu Jan 16 08:08:25 CET 2003


"David K. Trudgett" <dkt at registriesltd.com.au> wrote in message news:<mailman.1042687953.21581.python-list at python.org>...
> 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.)

Why not have optional, explicit fallthru?

print "On the %s day of Christmas, my true love gave to me" % ordinal(day)
switch day:
   case 12:
      print "12 drummers drumming,"
      fall
   case 11:
      print "11 pipers piping,"
      fall
   case 10:
      print "10 lords a-leaping,"
      fall
# etc.




More information about the Python-list mailing list