[Python-Dev] Simple Switch statement (original) (raw)
Raymond Hettinger raymond.hettinger at verizon.net
Sun Jun 25 02:30:00 CEST 2006
- Previous message: [Python-Dev] Simple Switch statement
- Next message: [Python-Dev] Simple Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Phillip Eby]
I would like to be able to use switches on types, enumerations, and the like.
Be careful about wanting everything and getting nothing. My proposal is the simplest thing that gets the job done for key use cases found in real code. Also, it is defined tightly enough to allow room for growth and elaboration over time. Good luck proposing some alternative that is explainable, has no hidden surprises, has an easy implementation, and allows fast hash-table style dispatch. Besides, if you want to switch on other types, it is trivial to include a reverse mapping (like that in the opcode.py example). Reverse mappings are to build and easy to read:
enumeration example
colormap = {} for code, name in enumerate('RED ORANGE YELLOW GREEN BLUE INDIGO MAGENTA'.split()): globals()[name] = code colormap[code] = name
def colormixer(color): switch colorname[color]: case 'RED', 'YELLOW', 'BLUE': handle_primary() case 'MAGENTA': get_another_color() default: handle_rest()
colormixer(RED) colormixer(ORANGE)
Raymond
- Previous message: [Python-Dev] Simple Switch statement
- Next message: [Python-Dev] Simple Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]