You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inconsistency in list-generator comprehension with yield(-from) #66163
bpo-10544: yield expression inside generator expression does nothing
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2021-12-11.11:04:09.084>created_at=<Date2014-07-11.23:06:27.360>labels= ['interpreter-core', 'type-bug']
title='inconsistency in list-generator comprehension with yield(-from)'updated_at=<Date2021-12-11.11:04:09.080>user='https://github.com/httpsgithubcomhakril'
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 !
list comprehension with yield(-from) would return a generator.
This is somewhat surprising, and I'm not sure it's expected/documented.
The example you provided seems to behave reasonably, so I don't think we should change the behavior (unless there is some actual bug in similar example). If anything, we could document this, but I'm not sure if it's worth doing it and where could be added.
generator comprehension would yield some None in the middle
of the expected values.
This also seems expected, and the behavior is consistent with the equivalent generator function.
It's a side effect of the hidden closure that provides the new scope for the iteration variable - that's an ordinary function object, so using yield or yield from turns it into a generator expression instead. Generator expressions are already generators, so using yield or yield from just adds more yield points beyond the implied ones.
I've never figured out a good way to document it - it's a natural consequence of the comprehension's closure. An explicit mention in the reference docs for comprehensions may be worth adding.
httpsgithubcomhakril commented on Jul 14, 2014
httpsgithubcomhakrilmannequin
MannequinAuthor
More actions
I found something else, I think it worth mentioning it.
This side-effect allows to create generators that return other values that None. And the CPython's behavior is not the same for all versions:
>>> {(yield i) : i for i in range(2)}
<generator object <dictcomp> at 0x7f0b98f41410>
>>> list(_)
# python3.(3,4,5)
[0, 1]
# python3.2
[0, 1, {None: 1}] # python3.2 appends the generator's return value.
I think this issue was resolved by now. This is what happens on 3.11:
>>> l = ["abc", range(3)]
>>> g = [(yield from i) for i in l]
File "<stdin>", line 1
SyntaxError: 'yield' inside list comprehension
>>> g2 = ((yield from i) for i in l)
File "<stdin>", line 1
SyntaxError: 'yield' inside generator expression
Activity
httpsgithubcomhakril commentedon Jul 11, 2014
Will playing with generators and
yield fromI 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 !
For
g1: it returns a generator because the listcomp contains ayield from.For
g2it append None because it yield the return value ofyield from i.It could be rewritten as:
def comp(x):
for i in x:
yield (yield from i)
ezio-melotti commentedon Jul 12, 2014
There seem to be two issues here:
This is somewhat surprising, and I'm not sure it's expected/documented.
The example you provided seems to behave reasonably, so I don't think we should change the behavior (unless there is some actual bug in similar example). If anything, we could document this, but I'm not sure if it's worth doing it and where could be added.
This also seems expected, and the behavior is consistent with the equivalent generator function.
ncoghlan commentedon Jul 13, 2014
It's a side effect of the hidden closure that provides the new scope for the iteration variable - that's an ordinary function object, so using yield or yield from turns it into a generator expression instead. Generator expressions are already generators, so using yield or yield from just adds more yield points beyond the implied ones.
I've never figured out a good way to document it - it's a natural consequence of the comprehension's closure. An explicit mention in the reference docs for comprehensions may be worth adding.
httpsgithubcomhakril commentedon Jul 14, 2014
I found something else, I think it worth mentioning it.
This side-effect allows to create generators that return other values that None. And the CPython's behavior is not the same for all versions:
>>> {(yield i) : i for i in range(2)} <generator object <dictcomp> at 0x7f0b98f41410> >>> list(_) # python3.(3,4,5) [0, 1] # python3.2 [0, 1, {None: 1}] # python3.2 appends the generator's return value.vstinner commentedon Jul 21, 2014
IMO it's a bug: [... for ... in ...] must create a list.
iritkatriel commentedon Dec 10, 2021
I think this issue was resolved by now. This is what happens on 3.11:
serhiy-storchaka commentedon Dec 11, 2021
Resolved in bpo-10544.