2014-02-11 17:44 GMT+01:00 Ned Batchelder <ned@nedbatchelder.com>:

If it is one, it’s a good thing to look like one.

well, you also get to remove the repeated variable name.

but a real switch statement should take advantage of python’s hashing. the best we have is

def _handle_spam():
    vomit()

def _handle_eggs():
    yum()

handlers = {
    'spam': _handle_spam,
    'eggs': _handle_eggs,
}

handlers[food]()

which makes me _handle_spam().


the problem of a real switch statement is apparently that there’s no good syntax:

switch food
case 'spam':
    ...

is strange due to the missing colon. we have that nowhere in python. yet

switch food:
case 'spam':
    ...

is strange because after the colon, everywhere else is an indentation. but

switch food:
    case 'spam':
        ...

is indentation overkill. nobody has found a good syntax yet.