python / cpython

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

Implement generator interface in itertools.chain. #60354

Closed f739b13a-720d-4143-a304-58707fd18fac closed 11 years ago

f739b13a-720d-4143-a304-58707fd18fac commented 11 years ago
BPO 16150
Nosy @gvanrossum, @rhettinger, @terryjreedy, @ncoghlan, @serhiy-storchaka
Files
  • itertools-chain-doc.diff: Change for element in it: yield element to yield from it in the documentation.
  • itertools-chain-send-throw-and-close-2.diff: Add send, throw, and close methods to itertools.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'] ```

    f739b13a-720d-4143-a304-58707fd18fac commented 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.

    f739b13a-720d-4143-a304-58707fd18fac commented 11 years ago

    Updated the patch. Thanks Serhiy Storchaka for comments on the previous one.

    gvanrossum commented 11 years ago

    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.

    gvanrossum commented 11 years ago

    I also don't think that this is a desirable feature.

    gvanrossum commented 11 years ago

    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.

    ncoghlan commented 11 years ago

    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.

    terryjreedy commented 11 years ago

    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'.