[Python-ideas] combining two threads: switch statements and inline functions

Andrew Barnert abarnert at yahoo.com
Wed Feb 12 00:47:43 CET 2014


On Feb 11, 2014, at 15:40, Ryan Gonzalez <rymg19 at gmail.com> wrote:

> That's an interesting idea. I'm not sure, though, whether that should be considered an inline function or a multi-line lambda.

Well, a general purpose multiline lambda would get you Bruce's proposal for free (well, for the cost of writing the word "lambda" or whatever the syntax is), but I think he's specifically proposing something more limited in hopes that it might be more acceptable (maybe more readable, maybe even not requiring as heavy syntax) if it weren't fully general purpose. 

That being said, I think this has the same problem as all multiline lambda type proposals: it embeds a statement in the middle of an expression, breaks the simple indentation rules, etc. It's not an accident that the provisional syntax is ugly; no one has yet come up with syntax that can solve those fundamental problems, and limiting the scope to a special type of expression doesn't seem to do anything relevant to help.
> 
> 
> On Tue, Feb 11, 2014 at 5:05 PM, 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]()
>> 
>> I don't know if there's a good idea here, but I do know that this pattern of writing dispatchable functions like this is fairly common, just as the switch pattern is. There are some obvious problems, most notably that in the switch statement, the scope of the statements is the same as the enclosing scope while in this code, each function has it's own scope. Notwithstanding that, I thought it was an interesting enough idea to share.
>> 
>> --- Bruce
>> Learn how hackers think: http://j.mp/gruyere-security
>> 
>> _______________________________________________
>> 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."
> 
> _______________________________________________
> 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/20140211/9a346a37/attachment-0001.html>


More information about the Python-ideas mailing list