python / cpython

The Python programming language
https://www.python.org
Other
63.22k stars 30.28k forks source link

yield unpacking #68515

Closed d5b9ee74-a580-412c-b8f5-619ccd976881 closed 9 years ago

d5b9ee74-a580-412c-b8f5-619ccd976881 commented 9 years ago
BPO 24327
Nosy @ethanfurman, @MojoVampire, @MitalAshok

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: ```python assignee = None closed_at = created_at = labels = ['type-feature'] title = 'yield unpacking' updated_at = user = 'https://github.com/MitalAshok' ``` bugs.python.org fields: ```python activity = actor = 'ethan.furman' assignee = 'none' closed = True closed_date = closer = 'serhiy.storchaka' components = [] creation = creator = 'Mital Ashok' dependencies = [] files = [] hgrepos = [] issue_num = 24327 keywords = [] message_count = 4.0 messages = ['244397', '244398', '244461', '244472'] nosy_count = 3.0 nosy_names = ['ethan.furman', 'josh.r', 'Mital Ashok'] pr_nums = [] priority = 'normal' resolution = 'out of date' stage = 'resolved' status = 'closed' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue24327' versions = ['Python 3.6'] ```

d5b9ee74-a580-412c-b8f5-619ccd976881 commented 9 years ago

(This is more of a feature request than a bug, but https://www.python.org/dev/peps/pep-0042/ said to post it here)

My request is to have syntax like this:

yield *iterable

to lazily return the iterable's items, not much unlike:

    # ...
    for i in iterable:
        yield i
    # ...

This is because I constantly find myself yielding all the values in, say, a list, then modifying it and yielding it in a loop.

ethanfurman commented 9 years ago

Do you mean something like:

--> def heh(): ... yield [1, 2, 3] ... --> for i in heh(): ... print(i) ... [1, 2, 3] # *grumble* --> def heh(): ... for i in [1, 2, 3]: ... yield i ... --> for i in heh(): ... print(i) ... 1 2 3

If so, use yield from:

--> def huh(): ... yield from [1, 2, 3] ... --> for i in huh(): ... print(i) ... 1 2 3

99ffcaa5-b43b-4e8e-a35e-9c890007b9cd commented 9 years ago

Should someone be closing this with "yield from already exists"?

ethanfurman commented 9 years ago

I was waiting a couple days to give Mital a chance to clarify if some other behavior was intended.