Word for a non-iterator iterable?

Discussion in 'Python' started by Leif K-Brooks, Feb 6, 2005.

  1. 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.
    Leif K-Brooks, Feb 6, 2005
    #1
    1. Advertising

  2. Leif K-Brooks wrote:
    > 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'?

    --
    Vincent Wehren
    vincent wehren, Feb 6, 2005
    #2
    1. Advertising

  3. Leif K-Brooks <> wrote:

    > 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
    Alex Martelli, Feb 6, 2005
    #3
  4. Leif K-Brooks

    Terry Reedy Guest

    "vincent wehren" <> wrote in message
    news:cu4kjo$d8s$1.nb.home.nl...
    > Leif K-Brooks wrote:
    >> 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?


    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
    Terry Reedy, Feb 7, 2005
    #4
  5. Leif K-Brooks

    Oren Tirosh Guest

    Leif K-Brooks <> wrote in message news:<>...
    > 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
    Oren Tirosh, Feb 7, 2005
    #5
  6. Leif K-Brooks

    Terry Reedy Guest

    "Oren Tirosh" <> wrote in message
    news:...
    > 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
    Terry Reedy, Feb 7, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,418
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    642
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,156
    Steven D'Aprano
    Apr 20, 2007
  5. Replies:
    2
    Views:
    357
    James Stroud
    Dec 10, 2008
Loading...

Share This Page