Closed f739b13a-720d-4143-a304-58707fd18fac closed 11 years ago
Since "yield from" made it into Python 3.3, I think it would be useful to chain multiple generators and still get a generator, not an iterator. That is, the following code:
def f():
yield from itertools.chain(A, B, C)
should be (at least roughly) equivalent to
def f():
yield from A
yield from B
yield from C
while still allowing to send() values to whichever subgenerator is currently running or throw() exceptions inside them.
The attached patch adds this functionality to itertools.chain objects.
Updated the patch. Thanks Serhiy Storchaka for comments on the previous one.
TBH I don't think that using "yield from" in the docs makes things clearer at all -- it just requires readers to understand a rather esoteric feature.
I also don't think that this is a desirable feature.
I am going to reject this unless at least one other core developer supports it. Extensive discussion on python-ideas has found very few supporters besides Serhiy Storchaka.
As discussed on python-ideas: the iterator interface is narrower than the generator interface. Tools for working with iterators are *expected* to lose the generator specific details, just as for loops do, especially when they deal with multiple iterators.
When Greg Ewing's PEP-3152 about cofunctions was discussed in the past, the possibility of a C level API to allow other objects to behave like generators was brought up - if such an API is ever adopted, then it *may* make sense to use it in itertools.
In the meantime, it's simpler if itertools is consistent about ignoring the .send(), .throw() and return value semantics of generators-as-coroutines.
In the python-ideas thread, "Propagating StopIteration value", Guido further elaborated "But that just seems to perpetuate the idea that you have, which IMO is wrongheaded. Itertools is for iterators, and all the extra generator features make no sense for it." I agree with this completely.
Given Raymond's consistent rejection of attempts to complexify itertools, I am 90% sure he would reject this also. (If he disagrees, it is easy to re-open.)
You and Serhiy seem to miss that the simplicity of the iterator interface as a lowest common denominator is a feature. It works for its intended purpose. It was fixed in 2.2 and so far not subject to change. And it can effectively be mixed in to other classes to produce iterator + whatever classes. Files are one example -- they are iterators, context managers, and io method objects. Generators, with its additions, are another example of augmented iterator.
"I think it would be useful to chain multiple generators and still get a generator, not an iterator." A generator is an instance of the generator class. As I understand the patch, chain would still produce chain objects, not generator objects, and hence would fail isinstance tests.
I suggested on the thread above that gentools should be a separate module, initially released on pypi. Unless it is easier than I think to produce generator instances in C, it should be coded in Python. That would be good anyway while developing the interface and behavior. Start with your generator chain function.
Perhaps it would help you to understand my viewpoint by considering the analogous statement "I think it would be useful to chain multiple files and still get a file, not an iterator.". Or substitute any other augmented iterator class for 'file'.
for element in it: yield element
toyield from it
in the documentation.send
,throw
, andclose
methods toitertools.chain
objects.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 = ['extension-modules', 'type-feature']
title = 'Implement generator interface in itertools.chain.'
updated_at =
user = 'https://bugs.python.org/pyos'
```
bugs.python.org fields:
```python
activity =
actor = 'terry.reedy'
assignee = 'none'
closed = True
closed_date =
closer = 'ncoghlan'
components = ['Extension Modules']
creation =
creator = 'pyos'
dependencies = []
files = ['27456', '27468']
hgrepos = []
issue_num = 16150
keywords = ['patch']
message_count = 7.0
messages = ['172204', '172280', '172422', '172423', '172436', '172437', '172438']
nosy_count = 6.0
nosy_names = ['gvanrossum', 'rhettinger', 'terry.reedy', 'ncoghlan', 'serhiy.storchaka', 'pyos']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue16150'
versions = ['Python 3.4']
```