Just took a look in the perl newsgroup....

John Griffiths reply at to.group.only.com
Tue May 20 10:29:50 EDT 2003


<snip/>
> But I don't think
> that the "case" statement is a similar issue -- I think you'll only
> find people who are new to Python pining for a case statement, since
> we already have at least _2_ of them:
>
>
>     # One simple approach
>     if x = 1:
>         do_something_1()
>     elif x = 2:
>         do_something_2()
>     elif x = 3:
>         do_something_3()
>
>
>     # Another (very powerful) approach
>     do_something = my_func_dict[x]
>     do_something()
>
> -- Michael Chermside
>
>

... but a case statement is constrained and discrete, your examples don't
show that.

 a lot depends on the available options for each branch allowed by the
language author.

i.e. if ',' and '..' was part of the syntax

case x
    of 1:
        # single value, same as if
        do_something_1()
    of 2, 3, 4:
        # selected values without taking up many if branches
        #    even though they are related in some way
        do_something_2()
    of 7..9:
        # range of values
        do_something_3()
    else
        # catch any other values
        # including out of context values for a status code
        do_something_else()

although it IS syntactic sugar;
    it saves excessive keystrokes,
    it is expressive,
    and I was taught that it increases the provability of code.

Regards John







More information about the Python-list mailing list