7

I was reading this question

How does this lambda/yield/generator comprehension work?

I would like to understand the language constructs in a bit more detail. Why are the brackets necessary?

(yield 55)

as opposed to

yield 55

Why does it only work in a generator expression when it is being passed as an argument to a function? If I do the following

 a = lambda x: 1
 a((yield 55))
 SyntaxError: 'yield' outside function
 # The error here implies that it must be used within a function... not a generator expression

When running the following:

a = lambda x: 1
list(a((yield 55)) for b in range(5))
[55,
 1,
 55,
 1,
 55,
 1,
 55,
 1,
 55,
 1]

Why does this yield produce a value? Who is calling next on it? The order of the evaluation is not clear by the syntax.

Har
  • 3,727
  • 10
  • 41
  • 75

0 Answers0