Since iterator objects work like sequences in several contexts, maybe they could support sequence-like operations such as addition. This would let you write for x in iter1 + iter2: do_something(x) instead of for x in iter1: do_something(x) for x in iter2: do_something(x) or the slightly better for i in iter1,iter2: for x in i: do_something(x) -- Sami Hangaslammi --
On Mon, 30 Jul 2001, Sami Hangaslammi <shang@cc.jyu.fi> wrote:
Since iterator objects work like sequences in several contexts, maybe they could support sequence-like operations such as addition. This would let you write
for x in iter1 + iter2: do_something(x)
instead of
for x in iter1: do_something(x)
for x in iter2: do_something(x)
or the slightly better
for i in iter1,iter2: for x in i: do_something(x)
No, instead of: class concat: def __init__(self, *iterators): self.iterators = list(iterators) def __iter__(self): return self def next(self): while self.iterators: try: return self.iterators[0].next() except StopIteration: del self.iterators[0] else: raise StopIteration for x in concat(iter1, iter2): do_something(x) (Note that the first n-2 lines can be refactored. Wasn't there talk about having an iterator module with useful stuff like that?) -- gpg --keyserver keyserver.pgp.com --recv-keys 46D01BD6 54C4E1FE Secure (inaccessible): 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6 Insecure (accessible): C5A5 A8FA CA39 AB03 10B8 F116 1713 1BCF 54C4 E1FE Learn Python! http://www.ibiblio.org/obp/thinkCSpy
Moshe Zadka wrote:
No, instead of:
class concat:
def __init__(self, *iterators): self.iterators = list(iterators)
def __iter__(self): return self
def next(self): while self.iterators: try: return self.iterators[0].next() except StopIteration: del self.iterators[0] else: raise StopIteration
for x in concat(iter1, iter2): do_something(x)
Or: from __future__ import generators def concat(*iterators): for i in iterators: for x in i: yield x for x in concat(iter1, iter2): do_something(x) Just
Just van Rossum wrote:
from __future__ import generators
def concat(*iterators): for i in iterators: for x in i: yield x
for x in concat(iter1, iter2): do_something(x)
Yes, this is the solution that I eventually ended up with too. However, 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). Moshe Zadka wrote:
Wasn't there talk about having an iterator module with useful stuff like that?
This would be a great idea. I've ended up with a sizeable bunch of small utility functions when playing around with generators/iterators in 2.2a1. -- Sami Hangaslammi --
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).
No. 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/)
Guido:
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.
I agree. The more special cases we add to try to make iterators look like sequences, the harder it's going to be to remember what you can and can't do with an iterator. Let's keep it as simple as possible. Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+
participants (5)
-
Greg Ewing
-
Guido van Rossum
-
Just van Rossum
-
Moshe Zadka
-
Sami Hangaslammi