[Python-ideas] switch statement as context manager?

Ryan Gonzalez rymg19 at gmail.com
Tue Feb 11 18:26:46 CET 2014


C and C++ have:

switch(value)
{
    case 1: do_stuff; break;
    case 2:
    case 3: do_stuff_2(); break;
    default: break;
}



On Tue, Feb 11, 2014 at 10:58 AM, Philipp A. <flying-sheep at web.de> wrote:

> 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.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple:
"It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was
nul-terminated."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140211/e1c45d7b/attachment.html>


More information about the Python-ideas mailing list