Another stab at a "switch/case" construct (for Python 3000):
Benjamin Schollnick
junkster at rochester.rr.com
Fri Mar 29 05:48:15 EST 2002
In article <slrnaa75sg.2gd.grey at teleute.dmiyu.org>,
Steve Lamb <grey at despair.dmiyu.org> wrote:
> case: Don't like it. We already have means to do what a case does and do it
> more elegantly. It would add a needless redundant portion to the language.
We do???
We have the IF .... ELIF statement structure, which can be used to
simulate a case statement, but it is not as clear nor as neat as
the case statement.
IF X == 0:
DO_THIS ()
ELIF X == 2:
DO_THIS (2)
ELIF X == 4:
DO_THIS (X+2)
ELIF Y == 6:
DO_THIS()
ELIF Z == "ZEBRA":
DO_THIS ( ZEBRA )
ELSE:
DO_NOT_DO_THIS ()
I am not aware of any "errors" in the above code, and it's totally
a BS routine... So please no pointing that out.
But, this routine is not exactly readable compared to a case:
CASE X of:
0 : DO_THIS()
2 : DO_THIS (2)
4 : DO_THIS (x+2)
6 : DO_THIS ()
ELSE:
DO_NOT_DO_THIS()
if Z == "ZEBRA":
DO_THIS (ZEBRA)
And it can be optimized to:
CASE X of:
0,6 : DO_THIS()
2 : DO_THIS (2)
4 : DO_THIS (x+2)
ELSE:
DO_NOT_DO_THIS()
if Z == "ZEBRA":
DO_THIS (ZEBRA)
The key issue is that I believe the IF / ELIF / ELSE statement can be
used with multiple variables during the IF ELIF..... At least I remember
using it that way....
The CASE is cleaner, and can only be used to compare a SINGLE variable
with multiple values. And I believe it's easier to maintain.....(And
can be optimized easier).
- Benjamin
More information about the Python-list
mailing list