<div dir="ltr">That's an interesting idea. I'm not sure, though, whether that should be considered an inline function or a multi-line lambda.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 11, 2014 at 5:05 PM, Bruce Leban <span dir="ltr"><<a href="mailto:bruce@leapyear.org" target="_blank">bruce@leapyear.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>What if we had the ability to write dictionaries with inline functions in them.</div><div><br></div>

<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="courier new, monospace">def sample(i, op, j):</font></div>

<div><font face="courier new, monospace">    switcher = {{</font></div><div><font face="courier new, monospace">       '-':: if i > j:</font></div><div><font face="courier new, monospace">                 return i - j</font></div>



<div><font face="courier new, monospace">             else:</font></div><div><font face="courier new, monospace">                 return j - i;;</font></div><div><font face="courier new, monospace">       '+':: return i + j;;</font></div>



<div><font face="courier new, monospace">    }}</font></div><div><font face="courier new, monospace">    return switcher[op]()</font></div></blockquote><br clear="all"><div><font face="arial, helvetica, sans-serif">Please don't pay attention to </font><font face="courier new, monospace">{{ :: ;; }}</font><font face="arial, helvetica, sans-serif"> 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:</font></div>



<div><font face="arial, helvetica, sans-serif"><br></font></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="courier new, monospace">def sample(i, op, j):</font></div></div><div><div>



<font face="courier new, monospace">    def diff():</font></div></div><div><div><span style="font-family:'courier new',monospace">        if i > j:</span></div></div><div><div><font face="courier new, monospace">            return i - j</font></div>



</div><div><div><font face="courier new, monospace">        else:</font></div></div><div><div><font face="courier new, monospace">            return j - i</font></div></div><div><div><font face="courier new, monospace">    def add():</font></div>



</div><div><div><font face="courier new, monospace">        return i + j</font></div></div><div><div><span style="font-family:'courier new',monospace">    switcher = {</span></div></div><div><div><div><font face="courier new, monospace">       '-': diff,</font></div>



</div></div><div><div><font face="courier new, monospace">       '+': add,</font></div></div><div><div><font face="courier new, monospace">    }</font></div></div><div><div><font face="courier new, monospace">    return switcher[op]()</font></div>



</div></blockquote><div><font face="courier new, monospace"><br></font></div><div><span style="font-family:arial,helvetica,sans-serif">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.</span><br>



</div><div><span style="font-family:arial,helvetica,sans-serif"><br></span></div><div><font face="arial, helvetica, sans-serif">--- Bruce<br></font><div><div><font face="arial, helvetica, sans-serif">Learn how hackers think: <a href="http://j.mp/gruyere-security" target="_blank">http://j.mp/gruyere-security</a></font></div>



</div></div>
</div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Ryan<div><div>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."</div>

</div><div><br></div></div>
</div>