10

Using Python 3.4, I get SyntaxError: invalid syntax here:

>>> xlist = [1,2,3,4,5]
>>> [yield(x) for x in xlist]
SyntaxError: invalid syntax

But this generates a generator object:

>>> [(yield(x)) for x in xlist]
<generator object <listcomp> at 0x00000076CC8E5DB0>

Are round brackets around yield required?

poke
  • 369,085
  • 72
  • 557
  • 602
GoGo
  • 559
  • 3
  • 7
  • 14
  • 2
    Its worth noting that the parentheses around `x` in your examples is not doing anything. `yield` is a keyword, not a function. – Blckknght Jun 15 '16 at 20:47
  • 2
    Don't use `yield` in a comprehension. It does something crazy that's never what you want, and it should really be a syntax error. – user2357112 Jun 15 '16 at 20:56
  • 1
    Epilogue: yield in a comprehension became a syntax error in 3.8 – VPfB Sep 01 '20 at 09:01

1 Answers1

15

The yield keyword can be used in two ways: As a statement, and as an expression.

The most common use is as a statement within generator functions, usually on its own line and all. It can be used like this:

yield <expr>
yield from <expr>

The yield expression however can be used wherever expressions are allowed. However, they require a special syntax:

(yield <expr>)
(yield from <expr>)

As you can see, the parentheses are part of the syntax to make yield work as an expression. So it’s syntactically not allowed to use the yield keyword as an expression without parentheses. That’s why you need to use parentheses in the list comprehension.

That being said, if you want to use list comprehension syntax to create a generator, you should use the generator expression syntax instead:

(x for x in xlist)

Note the parentheses instead of the square brackets to turn this from a list comprehension into a generator expression.


Note that starting with Python 3.7, yield expressions are deprecated within comprehensions and generator expressions (apart from within the iterable of the left-most for clause), to ensure that comprehensions are properly evaluated. Starting with Python 3.8, this will then cause a syntax error.

This makes the exact list comprehension in the question a deprecated usage:

>>> [(yield(x)) for x in xlist]
<stdin>:1: DeprecationWarning: 'yield' inside list comprehension
<generator object <listcomp> at 0x000002E06BC1F1B0>
poke
  • 369,085
  • 72
  • 557
  • 602