Where is CASE?????

Erik Max Francis max at alcyone.com
Tue Feb 20 12:41:17 EST 2001


Chris Withers wrote:

> Steve Purcell wrote:
>
> > Chris Richard Adams wrote:
> >
> > > Someone please tell me why I cannot find a CASE statement in the
> > > Python
> > > documentation index...
> >
> > Because there isn't one.
> >
> > Use if/elif/else instead.
> >
> > It doesn't sting as much as you might expect.
> 
> Oh but it does ;-)

Not particularly.  I come from C and C++, which have switch...case
statements, and it isn't a big deal for them to be missing.  Case
statements are realy nothing more than glorified repeated if...else
if... statements, and really don't have any more power (in fact, in C
and C++, case statements have less).

If you're really doing something where there would potentially be many,
many if...else if... clauses, then a better approach in Python would be
to use a dictionary whose keys are the case statements and whose values
are the data to retrieve or the function to call.  This is syntactically
a lot cleaner and allows for in-code modification of the transition
table.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Some mistakes we must carry with us.
\__/ Speaker-to-Animals
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.



More information about the Python-list mailing list