A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2006-June/066502.html below:

[Python-Dev] Simple Switch statement

[Python-Dev] Simple Switch statementRaymond Hettinger raymond.hettinger at verizon.net
Sun Jun 25 02:30:00 CEST 2006
[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





More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4