homepage

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: tuple unpacking on assignment should be lazy for unpacked generators
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Mario Wenzel, martin.panter
Priority: normal Keywords:

Created on 2015年11月03日 12:30 by Mario Wenzel, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (3)
msg253985 - (view) Author: Mario Wenzel (Mario Wenzel) Date: 2015年11月03日 12:30
if I have an assignment
a = <some_generator> then a is the generator. 
But if I do any kind of unpacking
*a, = <some_generator> # a list of all items
a, *aa = <some_generator> # first item in a, list of rest in as
If the right-hand side is a generator expression, I would expect that 
a, *aa unpacks the first element into a and puts the whole generator (having yielded the first element) back into aa.
So the star-operator of tuple unpacking with the right-hand-side having a generator should be lazy and not exhaust the whole generator.
msg253988 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015年11月03日 13:08
But when the starred element is not the final one, it couldn’t be lazy, so you would end up with an inconsistency.
a, *rest, z = <generator>
Also, it would be inconsistent with other kinds of iterables, which get turned into list objects.
>>> a, *aa = (1, 2)
>>> aa
[2]
>>> a, *aa = "abc"
>>> aa
['b', 'c']
msg254013 - (view) Author: Mario Wenzel (Mario Wenzel) Date: 2015年11月03日 19:36
You are right. I didn't even know 
head, *middle, end = <generator>
worked.
Thanks
History
Date User Action Args
2022年04月11日 14:58:23adminsetgithub: 69728
2015年11月03日 19:36:33Mario Wenzelsetstatus: open -> closed
resolution: rejected
messages: + msg254013
2015年11月03日 13:08:21martin.pantersetnosy: + martin.panter
messages: + msg253988
2015年11月03日 12:30:08Mario Wenzelcreate

AltStyle によって変換されたページ (->オリジナル) /