[Python-ideas] switch statement as context manager?

Philipp A. flying-sheep at web.de
Tue Feb 11 17:58:16 CET 2014


2014-02-11 17:44 GMT+01:00 Ned Batchelder <ned at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140211/bdc510ca/attachment-0001.html>


More information about the Python-ideas mailing list