5 ms·
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
by martin_k 15y ago
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.
- pyre 15y agoI'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.
- martin_k 15y agoYes, 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.
- cool-RR 15y agoSure, I understand all of that. I'm just saying it's unusual to see a lambda function throwing away the value like that.
- martin_k 15y agoThe value of the lambda is the generator object. How else could you call list() on it if it was thrown away?
- cool-RR 15y agoYou're just using the word "value" in a different way. I meant "value" as in the 7.
- martin_k 15y agoYou'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.