Sunday, May 01, 2005

ASPN : Python Cookbook : Readable switch construction without lambdas or dictionaries

ASPN : Python Cookbook has a recipe for an alternative switch construct in Python.

Creates a class 'switch', and then the construct is

for case in switch(variable):
if case('a'):
appropriate action
break
if case('b'):
appropriate action
...

elif can be used instead of if and eliminates need for break, but the if construct also allows pass to be the appropriate action and this allows fall-through.

0 Comments:

Post a Comment

<< Home