<div dir="ltr"><div class="gmail_quote">On Sat, Dec 17, 2011 at 6:14 PM, Shai Berger <span dir="ltr">&lt;<a href="mailto:shai@platonix.com">shai@platonix.com</a>&gt;</span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">
&gt;<br>
&gt; (Note that these are the only commands that I ran. You&#39;re not allowed to<br>
&gt; run any other commands before them.)<br>
&gt;<br>
</div>&gt; The riddle: What&#39;s the shortest thing you can put instead of *???* so my<br>
<div class="im">&gt; second command would not raise an exception?<br>
&gt;<br>
&gt;<br>
</div>???= (yield)<br>
<br>
right?<br>
<br>
(mailed privately, to avoid ruining the fun...)<br>
</blockquote></div><br><div>Yep!!! I just almost finished writing the email to tell everyone that when I got your answer.</div><div><br></div><div>Congrats for solving the riddle Shai.</div><div><br></div><div>So as Shai said, the solution is:<br>

<div> </div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><b></b>&gt;&gt;&gt; f = lambda: g(<b>(yield)</b>)<br>

&gt;&gt;&gt; f()  </blockquote><div><br>Funny, isn&#39;t it? I was surprised to see that the `yield` keyword can be used in a lambda function. <br><br>So when you type `f()`, it just returns a generator. If you&#39;ll try to exhaust it, an exception will be raised because `g` doesn&#39;t exist, but that&#39;s a new line :)</div>

</div><div><br></div><div>It&#39;s funny that in this case, Python seems to throw away the value of the lambda function! As we know, the `yield` keyword actually forms a statement whose value is `None`, unless you used the generator&#39;s `.send` instead of `.next`. So you could also use `.send` to send in whatever value you want into the lambda function, and Python will just throw it away. Unless I&#39;m missing something.</div>

<div><br></div><div>So that&#39;s the only case I can think of where Python completely throws away the value of a lambda function.</div><div><br></div><div><br></div><div>Another funny thing that I learned from this riddle is that when you do a function invocation in Python, Python accesses the function <i>before</i> it looks at the arguments.</div>

<div><br></div><div>So if were to do:</div><div><br></div><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

adfgadgof(1 / 0)</blockquote><div><br></div><div>Python will complain about the non-existent function before it even sees the division-by-zero.</div></div>