<div dir="ltr"><div class="gmail_quote">On Sat, Dec 17, 2011 at 6:14 PM, Shai Berger <span dir="ltr"><<a href="mailto:shai@platonix.com">shai@platonix.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">
><br>
> (Note that these are the only commands that I ran. You're not allowed to<br>
> run any other commands before them.)<br>
><br>
</div>> The riddle: What's the shortest thing you can put instead of *???* so my<br>
<div class="im">> second command would not raise an exception?<br>
><br>
><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>>>> f = lambda: g(<b>(yield)</b>)<br>
>>> f() </blockquote><div><br>Funny, isn'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'll try to exhaust it, an exception will be raised because `g` doesn't exist, but that's a new line :)</div>
</div><div><br></div><div>It'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'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'm missing something.</div>
<div><br></div><div>So that'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>