[Python-ideas] combining two threads: switch statements and inline functions
Andrew Barnert
abarnert at yahoo.com
Wed Feb 12 00:50:02 CET 2014
On Feb 11, 2014, at 15:45, Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Feb 12, 2014 at 10:05 AM, Bruce Leban <bruce at leapyear.org> wrote:
>> What if we had the ability to write dictionaries with inline functions in
>> them.
>>
>> def sample(i, op, j):
>> switcher = {{
>> '-':: if i > j:
>> return i - j
>> else:
>> return j - i;;
>> '+':: return i + j;;
>> }}
>> return switcher[op]()
>>
>>
>> Please don't pay attention to {{ :: ;; }} which would never be the actual
>> syntax or what this code is doing (nothing useful) or whether i or j should
>> be passed as parameters rather than closure. This is equivalent to:
>>
>> def sample(i, op, j):
>> def diff():
>> if i > j:
>> return i - j
>> else:
>> return j - i
>> def add():
>> return i + j
>> switcher = {
>> '-': diff,
>> '+': add,
>> }
>> return switcher[op]()
>
> Hmm. In terms of scoping rules and so on, would it be cleaner to
> instead make it more equivalent to a big fat expression?
>
> Hmm. In terms of scoping rules and so on, would it be cleaner to
> instead make it more equivalent to a big fat expression?
That just means it's a lambda form without the lambda keyword, right? That's certainly cleaner, but I'm not sure what it buys.
More information about the Python-ideas
mailing list