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

Russell E. Owen no at spam.invalid
Tue May 20 14:50:43 EDT 2003


In article <badkce$32m$2 at news7.svr.pol.co.uk>,
 "John Griffiths" <reply at to.group.only.com> wrote:

>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.

I'm afraid I don't understand what you mean by "constrained and 
discrete", so I may be on thin ice here, but the following Python code 
seems about as clear:

if x == 1:
  do_something_1()
elif x in (2, 3, 4):
  do_something_2()
elif 7 >= x >= 9: # or "x in (7,8.9)" or "x in range(7,10)" or...
   do_something_3()
else:
  do_something_else()

When you have multiple values that can trigger the same action then I'd 
probably stick to the above rather than use objects or dictionaries of 
functions.

-- Russell




More information about the Python-list mailing list