This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: List reports incorrect length if modifed after yield
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, matrixise, robinh
Priority: normal Keywords:

Created on 2016-08-08 08:49 by robinh, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg272151 - (view) Author: Robin (robinh) Date: 2016-08-08 08:49
reproduction script below. In the last print statement, it shows me a list with items in it, but with a length of 0

def generator():
	l = []
	yield l
	l.append(1)
	# this correctly prints 1
	print(len(l))

# this should print [([1], 1)], but actually gives [([1], 0)]
print([(l, len(l)) for l in generator()])
msg272152 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2016-08-08 09:02
when you yield, the list is empty, in this case, the length of your list is just 0 and not 1.
msg272153 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2016-08-08 09:05
At the time the len function in list comprehension is called .append has not executed, the len call in list comprehension operates on object as it is, whereas the object itself is only referenced rather than copied. If you were to copy the yielded list, then a more expected behaviour of returning [([], 0)] would be demonstrated.
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71894
2016-08-08 09:05:26SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg272153

resolution: not a bug
stage: resolved
2016-08-08 09:02:52matrixisesetstatus: closed -> open
2016-08-08 09:02:10matrixisesetstatus: open -> closed
nosy: + matrixise
messages: + msg272152

2016-08-08 08:49:01robinhcreate