[Python-ideas] generator.__add__

Nick Coghlan ncoghlan at gmail.com
Mon Apr 5 14:59:23 CEST 2010

Guido van Rossum wrote:
> On Sun, Apr 4, 2010 at 9:58 AM, cool-RR <cool-rr at cool-rr.com> wrote:
>> I'm still a bit confused by generators and generator expressions, but what
>> about making a `generator.__add__` method, that'll act like
>> `itertools.chain`?
> This gets proposed several times a year. We always shoot it down for
> the same reasons: iterators are an abstract API and we don't want to
> burden every iterator implementation with having to implement various
> operations.

I actually messed around with writing a FlexIter class once that wrapped
an arbitrary iterator and mapped __add__ to itertools.chain, __mul__ to
itertools.repeat and a few other things. It actually turned out to get
really ugly, really fast, so I abandoned the idea.

Making arbitrary iterators as easy to manipulate as lists is an
appealing idea, but a lot harder than it sounds given the deliberately
limited definition of the iterator protocol.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list