Word for a non-iterator iterable?

L

Leif K-Brooks

Is there a word for an iterable object which isn't also an iterator, and
therefor can be iterated over multiple times without being exhausted?
"Sequence" is close, but a non-iterator iterable could technically
provide an __iter__ method without implementing the sequence protocol,
so it's not quite right.
 
V

vincent wehren

Leif said:
Is there a word for an iterable object which isn't also an iterator, and
therefor can be iterated over multiple times without being exhausted?
"Sequence" is close, but a non-iterator iterable could technically
provide an __iter__ method without implementing the sequence protocol,
so it's not quite right.

How about 'reiterable'?
 
A

Alex Martelli

Leif K-Brooks said:
Is there a word for an iterable object which isn't also an iterator, and
therefor can be iterated over multiple times without being exhausted?
"Sequence" is close, but a non-iterator iterable could technically
provide an __iter__ method without implementing the sequence protocol,
so it's not quite right.

Not just ``technically'' -- a dict is a good and very common example of
just such a "non-iterator, non-sequence iterable".

As you're focusing on "can be iterated over multiple-times", I like
"re-iterable"; it centers on what you can DO with the object, rather
than quibbling what it ISN'T;-)


Alex
 
T

Terry Reedy

The 'therefor' above is a non sequitor. A non-iterator iterable can also
be non-reiterable.
How about 'reiterable'?

I agree. Reiterability is essential for some algorithms, while I am hard
put to think of a situation in which non-iterator-ness, by itself, is.

Terry J. Reedy
 
O

Oren Tirosh

Leif K-Brooks said:
Is there a word for an iterable object which isn't also an iterator, and
therefor can be iterated over multiple times without being exhausted?
"Sequence" is close, but a non-iterator iterable could technically
provide an __iter__ method without implementing the sequence protocol,
so it's not quite right.

"reiterable". I think I was the first to use this word on
comp.lang.python.

If you have code that requires this property might want to use this
function:

..def reiter(x):
.. i = iter(x)
.. if i is x:
.. raise TypeError, "Object is not re-iterable"
.. return i

example:

..for outer in x:
.. for inner in reiter(y):
.. do_something_with(outer, inner)

This will raise an exception when an iterator is used for y instead of
silently failing after the first time through the outer loop and
making it look like an empty container.

When iter() returns a new iterator object it is a good hint but not a
100% guarantee that the object is reiterable. For example, python 2.2
returned a new xreadlines object for iterating over a file but it
messed up the underlying file object's state so it still wasn't
reiterable. But when iter() returns the same object - well, that's a
sign that the object is definitely not reiterable.

Oren
 
T

Terry Reedy

Oren Tirosh said:
But when iter() returns the same object - well, that's a
sign that the object is definitely not reiterable.

Unless, of course, __iter__ resets the iteration variable to its starting
value. But it would definitely not be simultaneously doubly iterable, as
one would want to compute a self crossproduct. And one could object that
such an __iter__ is not proper for an iterator (return self and do nothing
else). So such an __iter__ should better be written, perhap as a generator
function, to return an independent object with independently initialized
iteration variable.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top