[Python-Dev] Iterator addition?

Guido van Rossum guido@zope.com
Mon, 30 Jul 2001 08:42:49 -0400

> the real point I was trying to raise was, wether interators should look
> like sequences regarding addition, since the two are already exchangeable
> in so many places (e.g. tuple unpacking).


Adding a + operator would conflict in the case an iterator is also a
user-defined object.  Adding a * operator can't work because an
iterator cannot be restarted (you have to extract the iterator afresh
from the original object).  Adding any other sequence operation
(slicing, indexing, len() etc.) flies in the face of the
"forward-only" nature of iterators.

The *only* thing that iterators and sequences have in common is that
they can be iterated over.  So they are substitutable in all context
where that's all you do -- including sequence (not tuple!) unpacking.
And not in any other contexts.

--Guido van Rossum (home page: http://www.python.org/~guido/)