Word for a non-iterator iterable?
Oren Tirosh
oren at REMOVETHIS1.hishome.net
Mon Feb 7 14:10:50 EST 2005
Leif K-Brooks <eurleif at ecritters.biz> wrote in message news:<36lu1cF516qtdU1 at individual.net>...
> 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
More information about the Python-list
mailing list