Python tracks output as function argument

I read this question

How does this understanding of lambda / yield / generator work?

I would like to understand the language constructs in more detail. Why are parentheses needed?

(yield 55) 

Unlike

 yield 55 

Why does it only work in a generator expression when it is 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 

At startup:

 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 make a difference? Who is calling next? The order of evaluation is not clear on the syntax.

+7
python
source share

No one has answered this question yet.

See similar questions:

54
output in generator list views and expressions
49
How does this understanding of lambda / output / generator work?

or similar:

9540
What does the yield keyword do?
5504
Does Python have a ternary conditional operator?
5231
What are metaclasses in Python?
4473
Calling an external command in Python
3790
How can I safely create a subdirectory?
3602
Does Python have a "contains" substring method?
2840
Using global variables in functions
2818
Finding an index of an element with a list containing it in Python
2425
Least Surprise and Mutable Default Argument
1420
Hidden features of Python

All Articles