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 <>:
On Sat, Apr 26, 2014 at 11:03 PM, Philipp A. <> 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'.

Python-ideas mailing list
Code of Conduct: