[Python-ideas] Yet Another Switch-Case Syntax Proposal

Philipp A. flying-sheep at web.de
Sat Apr 26 15:43:27 CEST 2014


sure it works if `eggs` has a `__iadd__` method. why shouldn’t it use the
outer local?

but that’s irrelevant. as i said: what i did is closest thing. yet in some
other respects, if/else is closer, as there is no real alternative to a
switch statement in python.

my only point is that apart from being cleaner, a switch block also behaves
differently than a if/else chain, and performs better.


2014-04-26 15:27 GMT+02:00 Chris Angelico <rosuav at gmail.com>:

> On Sat, Apr 26, 2014 at 11:03 PM, Philipp A. <flying-sheep at web.de> wrote:
> > in python, the most similar thing is something like this:
> >
> >     'key1': lambda: eggs += lay_eggs(), #branch 1
>
> Except that lambda doesn't create a code block, it creates a nested
> function. This example won't work, and nor will anything else that
> needs to mutate locals; you'd have to declare an out-of-line function
> (as with branch_2_3) to be able to assign to 'nonlocal eggs'.
>
> ChrisA
> _______________________________________________
> 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/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140426/e2fecf16/attachment.html>


More information about the Python-ideas mailing list