[issue21964] inconsistency in list-generator comprehension with yield(-from)

hakril report at bugs.python.org
Sat Jul 12 01:06:27 CEST 2014


New submission from hakril:

Will playing with generators and `yield from` I found some inconsistency.

list comprehension with yield(-from) would return a generator.
generator comprehension would yield some None in the middle of the expected values.

Examples:
    l = ["abc", range(3)]
    g1 = [(yield from i) for i in l]
    print(g)
    <generator object <listcomp> at 0x7f5ebd58b690>
    print(list(g))
    ['a', 'b', 'c', 0, 1, 2]  # this result is super cool !

    g2 = ((yield from i) for i in l)
    print(g2)
    <generator object <genexpr> at 0x7f5ebd58b6e0>
    print(list(g2))
    ['a', 'b', 'c', None, 0, 1, 2, None]


For `g1`: it returns a generator because the listcomp contains a `yield from`.

For `g2` it append None because it yield the return value of `yield from i`.
It could be rewritten as:
    def comp(x):
        for i in x:
            yield (yield from i)

----------
components: Interpreter Core
messages: 222811
nosy: hakril
priority: normal
severity: normal
status: open
title: inconsistency in list-generator comprehension with yield(-from)
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21964>
_______________________________________


More information about the Python-bugs-list mailing list