<div dir="rtl"><div dir="ltr">Sorry, I probably wasn't clear enough.</div><div dir="ltr">My idea was to add these method (__add__, __getitem__) to the already available iterators (map,zip,dict.items) and generators by default. Not to make these methods part of the iterator protocol.</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div dir="ltr">2015-01-03 17:28 GMT+02:00 Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span>:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Sat, Jan 03, 2015 at 03:37:03PM +0200, yotam vaknin wrote:<br>
> I would like to purpose that in python 3 it will be easy to chain and slice<br>
> Iterators just like you can add and slice lists and tuples easily.<br>
><br>
> I think there could be 2 methods to do this:<br>
> 1. Implementing '+' and slicing ([1:2:3]) for iterators and generators by<br>
> default, Resulting inĀ  Itertool's chain(a,b) and islice(a,b,c) respectively.<br>
> 2. Having itertool's chain and islice imported by default.<br>
<br>
</span>The iterator protocol is intentionally simple. To create an iterator,<br>
you need to only define two methods:<br>
<br>
(1) __iter__, which returns the instance itself;<br>
<br>
(2) __next__, which returns the next item and raises StopIterator when<br>
done.<br>
<br>
<br>
With your proposal, you would have to define two or three more methods:<br>
<br>
(3) __add__ and possibly __radd__, to chain iterators;<br>
<br>
(4) __getitem__, for slicing.<br>
<br>
There is nothing stopping you from adding these methods to your own<br>
iterator classes, but with your proposal that would be compulsory for<br>
all iterators.<br>
<span class=""><br>
<br>
> I think since python 3 return zip,map, dict.items and so on as iterators,<br>
> it makes working with those kind of objects more difficult without having<br>
> these methods around. And since those methods are important enough to have<br>
> for lists, it seems important enough for iterators too.<br>
<br>
</span>It only takes a single line of code to get iterator chaining and<br>
slicing:<br>
<br>
from itertools import chain, islice<br>
<br>
And now you can chain and slice any iterator, regardless of where it<br>
came from. I don't think that is difficult.<br>
<br>
*If* iterators where instances of a concrete base class, then it would<br>
make sense to add chain and slice methods to that base class so all<br>
other iterators could inherit from it. But they aren't, iterators in<br>
Python are values which obey a protocol, not inheritence. That means<br>
that a functional approach, like itertools, is more appropriate.<br>
<br>
I can see the appeal of using + and [a:b:c] syntax instead of function<br>
syntax chain() and islice(), but I don't think the advantage is enough<br>
to out-weigh the costs.<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</font></span></blockquote></div><br></div>