[Tutor] Re: case/switch, etc.

Charlie Clark charlie@begeistert.org
Fri, 11 Oct 2002 20:21:21 +0200


Alan wrote:

That'll be faster too.
The only snag is that you have a lot of very small functions
to write. If thats a problem you might be able to use lambdas,
however they are restricted to expressions!:

cases={test1: lambda : sqrt(foo),
       test2: lambda : pow(foo,3),
       test3: lambda : foo % 1,...}

if test in cases.keys():
   result = cases[test]()
else: raise CaseError

personally, I'd probably just use if/elif ;-)

***
never having worked with case/switch, etc. I'm not sure when you would like to use it but I think it 
should also be mentioned that apart from if/elif you can also use try/except combinations instead for 
fine-grained control and powerful return values, etc. 

try:
	do_something()
except MyError:
       do_something_else()

Charlie