Andrew Durdin wrote: > In this case the dictionary is obviously a better and clearer choice. > I've generally found for other circumstances where I've used switch > statements that the code ends up more readable if it's reorganised so > that the switch statements are all of the form above, or are > eliminated entirely--so I don't miss a switch/case statement in > Python.
I find the lack of a switch statement encourages me to write data-driven programs (which is a god-send when I have to enhance them later to deal with additional cases). The thing I love most about Python is the fact that callables can be slung around at run-time, just like any other object. So, I set up (usually for command-line options) mappings from options to callables which define "how to perform action x". Then as the options are translated, I query the lookup tables, and store the results in appropriately named variables (e.g "create_connection", "connect_link" for a program I use to test serial data circuits with a variety of connections and links running over different hardware). The main body of the program is then comparatively trivial, because it just calls methods whose names describe (in a high-level way) what they do. A new connection or link type can be handled just by adding an entry to the appropriate dictionary. Basically, it's the GoF Strategy pattern, without all the pain that's required to set it up in static languages. Cheers, Nick. -- http://mail.python.org/mailman/listinfo/python-list