Another stab at a "switch/case" construct (new proposal):

Cliff Wells logiplexsoftware at earthlink.net
Fri Mar 29 18:02:25 EST 2002


On 29 Mar 2002 14:35:25 -0800
Ken Peek wrote:

> branchon MyObject:
> 
>     [Object_1, Object_N, (99,98,MyVar), 42]:
>         # do something
>         # do more
>         fallthrough # (fall through to next clause body)
>     [[1,2,3], "hello", MyDynamicObject, MyList]:
>         # do something
>         # do more
> 
>     []:
>         # this matches if "MyObject" is an empty list
>         # do something
>         # do more
> 
>     else:
>         # we did not have any matching clauses
>         # do something
>         # do more
> 
> ==================================
> 
> OK-- please send in the next barrage of critisism...

OK ;)  /If/ a switch/case style test were to be added to Python, the
fallthrough should not be included.  Although not falling through by
default is better than having to explicitly not fall through (ala C), it
still makes the code less clear.  If there is common code to be shared
between cases, put it in a function.

I still feel that the need for this type of construct is unnecessary most
of the time.  Special-case code should be encapsulated within an object. 
Contrast the following:

switch type(object):
    int: 
        print "%d" % object
    str: 
        print "%s" % object
    list: 
        print "[",
        for i in object:
            print i,
        print "]"

with:

print object

Now which is cleaner?   I think the perceived need for this type of
construct is a holdover from thinking in other programming languages versus
thinking in Python.

-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308




More information about the Python-list mailing list