News for hackers. Stuff that matters.
Re:Silly Python riddle by cool-RR on Tuesday December 20, 2011 @10:12AM (#3373678)
I think you misunderstood which value gets thrown away.

Consider this:

    >>> g = lambda x: 7
    >>> f = lambda: g((yield))
    >>> list(f())
    [None]
Can you tell me where the 7 went to? As far as I see, it went into oblivion.
Re:Silly Python riddle by ul5255 on Tuesday December 20, 2011 @12:10PM (#3374177)
Strange. With Python 2.6 I get [None, 7] while with Python 2.7 it is [None]. Looking at the OP codes the 2.7 one's look weird.

Python 2.6:

  >>> dis.dis(f)
    1           0 LOAD_GLOBAL              0 (g)
                3 LOAD_CONST               0 (None)
                6 YIELD_VALUE
                7 CALL_FUNCTION            1
               10 RETURN_VALUE
Python 2.7:

  >>> dis.dis(f)
    1           0 LOAD_GLOBAL              0 (g)
                3 LOAD_CONST               0 (None)
                6 YIELD_VALUE
                7 CALL_FUNCTION            1
               10 POP_TOP
               11 LOAD_CONST               0 (None)
               14 RETURN_VALUE
In essence the Python 2.7 compiler decides to throw away the result of the g() function call and replaces it with None. Does anyone understand why?
Re:Silly Python riddle by martin_k on Tuesday December 20, 2011 @12:26PM (#3374248)
Re:Silly Python riddle by ul5255 on Tuesday December 20, 2011 @02:38PM (#3374787)
Ahh, thanks!
Re:Silly Python riddle by martin_k on Tuesday December 20, 2011 @10:26AM (#3373723)
As i said, f() returns a generator object in this case. So you're calling list on a generator that yields None once, the result of which is a list that contains None. If you change the `yield` to `yield "foobar"`, list(f()) will get you `["foobar"]`.

Edit: Perhaps, to clear things up more, about where the value 7 went- your snippet can be expressed without using lambdas as

  >>> def g(x):
  ...  return 7
  ... 
  >>> def f():
  ...  v = (yield)
  ...  g(v)
  ... 
  >>> list(f())
  [None]
Note that there's no return before g(v) because generators (at least in 2.7) can only yield, not return a value.
Re:Silly Python riddle by pyre on Tuesday December 20, 2011 @10:33AM (#3373751)
I'm thinking that the parent is expecting 'yield' to return 'None' into g(), and then for g() to return 7 since it doesn't do anything with the 'x' parameter, and is confused why that isn't the case.
Re:Silly Python riddle by martin_k on Tuesday December 20, 2011 @10:56AM (#3373822)
Yes, but I think this is cleared up if you consider that calling a generator function does not execute its body, but return generator object. It doesn't really matter if it's a lambda or a regular named function.
Re:Silly Python riddle by cool-RR on Tuesday December 20, 2011 @10:45AM (#3373786)
Sure, I understand all of that. I'm just saying it's unusual to see a lambda function throwing away the value like that.
Re:Silly Python riddle by martin_k on Tuesday December 20, 2011 @10:59AM (#3373838)
The value of the lambda is the generator object. How else could you call list() on it if it was thrown away?
Re:Silly Python riddle by cool-RR on Tuesday December 20, 2011 @11:38AM (#3374035)
You're just using the word "value" in a different way. I meant "value" as in the 7.
Re:Silly Python riddle by martin_k on Tuesday December 20, 2011 @11:48AM (#3374084)
You're right. I am still referring to that particular part of your blog post:

  >> So that’s the only case I can think of where Python
  >> completely throws away the value of a lambda function.