1

Why the result is different?

In [19]: [(yield from (1, 2)) for x in [1]]
Out[19]: <generator object <listcomp> at 0x0438B3C0>

In [20]: list(_)
Out[20]: [1, 2]

In [21]: ((yield from (1, 2)) for x in [1])
Out[21]: <generator object <genexpr> at 0x0438B480>

In [22]: list(_)
Out[22]: [1, 2, None]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nestandy
  • 31
  • 3
  • As that answer explains, it's somewhat of a bug, the `None`s come from the yield inside the generator. – cs95 Jul 20 '17 at 08:34
  • The list comprehension also produces the `None`, you just don't see it added to the list. This is because using `yeld` or `yield from` inside a list comprehension or a generator expression is actually *not supported*. The language grammar permits it but the compiler produces non-sensical bytecode for the code in question. – Martijn Pieters Jul 20 '17 at 08:45

0 Answers0