Another stab at a "switch/case" construct (for Python 3000):
Steve Holden
sholden at holdenweb.com
Fri Mar 29 08:37:53 EST 2002
"Benjamin Schollnick" <junkster at rochester.rr.com> wrote ...
> 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.
>
[case statement suggestions ]
>
> 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()
>
OK, how would the above handle the case where X == (0,6)?
Clearly we could explicitly add parentheses to handle tuple-valued case
variables, but would we then have to also lable the singleton case values as
tuples?
case X of:
(0, 6, (0,6)): this()
(2,): that()
(4,): the_other()
Doesn't look quite so clean now, though, does it?
> 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....
>
I'm not sure what this statement means. With an "if" construct you can
compare as few or as many variables as you like to form a single Boolean
result. And clearly if/elif/elif/else can handle any number of cases anyway.
Ultimately the only way to implement a case statement in its full generality
is to use the lower-level if/elif/elif/else construct, so what we are
discussing here is syntactic sugar.
> 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).
>
I don't. By the way, there's NO NEED TO SHOUT! ;-)
regards
Steve
More information about the Python-list
mailing list