Fwd: [Tutor] using exceptions to implement case/switch?

Kent Johnson kent_johnson at skillsoft.com
Fri Oct 15 23:47:03 CEST 2004


Bill,

I'm really confused now. Are you trying to duplicate the visual appearance 
of a case statement? Your solution hasn't eliminated the conditionals, it 
just hides them in a helper function. It adds the overhead of throwing and 
catching an exception.

If your question is, how do I do this the Python way? - the usual solution 
is either chained conditionals (if / elif / ... / else) or using a dict as 
a dispatcher. If your question is, will this work? - yes, it will work, but 
I sure don't understand why you would want to do it that way.

Kent

At 02:32 PM 10/15/2004 -0700, Bill Campbell wrote:
>On Fri, Oct 15, 2004, Max Noel wrote:
> >(sigh... Forgot to click "reply to all". It'd be more practical if that
> >list set a reply-to header IMO...)
>
>Funny, my archaic mailer works just fine replying to lists by
>pressing ``L''.
>
> >Begin forwarded message:
> >
> >>From: Max Noel <maxnoel_fr at yahoo.fr>
> >>Date: October 15, 2004 21:42:37 BST
> >>To: Bill Mill <bill.mill at gmail.com>
> >>Subject: Re: [Tutor] using exceptions to implement case/switch?
> >>
> >>
> >>On Oct 15, 2004, at 21:36, Bill Mill wrote:
> >>
> >>>Bill,
> >>>
> >>>I think you've misunderstood the example; pardon me if I've
> >>>misunderstood you in turn, but Kent's dispatcher will run the
> >>>functions in the current namespace. No new processes or namespaces
> >>>will be created, making your lambda method redundant.
> >>>
> >>>Peace
> >>>Bill Mill
> >>>bill.mill at gmail.com
> >>
> >>      I think he meant scope, not namespace. However IIRC lambda functions
> >>have their own scope, like normal functions (not 100% sure of that, I
> >>never use lambdas), thus the method is redundant either way.
>
>My main goal had little to do with lamdas or function calls, but the desire
>to implement a construct that closely resembles C switch or other
>language's case statements.  How the exceptions are raised isn't important.
>The important thing is using multiple ``except'' sections after a ``try''
>instead of using conditional tests.
>
>Bill
>--
>INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
>UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
>FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
>URL: http://www.celestial.com/
>
>With Congress, every time they make a joke it's a law; and every time
>they make a law it's a joke.
>                 -- Will Rogers
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list