Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between callable-iterator and listiterator and iterator?

There are multiple iterator classes depending on what you're iterating over:

>>> import re
>>> re.finditer("\d+", "1 ha 2 bah").__class__
<type 'callable-iterator'>
>>> iter([1, 2]).__class__
<type 'listiterator'>
>>> iter("hurm").__class__
<type 'iterator'>

Two questions:

  1. Is there any meaningful distinction between them?
  2. Why is the first one called a callable-iterator? You definitely cannot call it.
like image 458
Claudiu Avatar asked Oct 10 '13 04:10

Claudiu


People also ask

What is difference between Iterator and ListIterator and enumeration in Java?

iterator(); ListIterator is the most powerful cursor among all the three cursors. ListIterator is only applicable for list implemented classes like ArrayList, LinkedList, Stack, etc. ListIterator traverses both in the forward and backward direction.

What is the relationship between Iterator and ListIterator interface?

An Iterator is an interface in Java and we can traverse the elements of a list in a forward direction whereas a ListIterator is an interface that extends the Iterator interface and we can traverse the elements in both forward and backward directions.

What is the difference between Iterator and enhanced for?

The difference is largely syntactic sugar except that an Iterator can remove items from the Collection it is iterating. Technically, enhanced for loops allow you to loop over anything that's Iterable, which at a minimum includes both Collections and arrays.

What is the difference between Iterator and iterable in Java?

Iterable doesn't store any iteration state. Iteration state is the pointer to the element in a collection which the iterator is going to return next. Iterator instance maintains the iteration state. This implies the user can check if the next element exists, move forward to the next element, etc.


2 Answers

BrenBarn answers #1 quite delightfully, but I believe I have unlocked the mysteries of #2. To wit, a callable-iterator is that which is returned for using iter with its second form:

>>> help(iter)
iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator

    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.
    In the second form, the callable is called until it returns the sentinel.

To wit:

>>> def globals_are_bad_mmkay():
        global foo
        foo += 1
        return foo

>>> foo = 0
>>> it = iter(globals_are_bad_mmkay, 10)
>>> it
<callable-iterator object at 0x021609B0>
>>> list(it)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
like image 171
Claudiu Avatar answered Sep 25 '22 15:09

Claudiu


Being an iterator means implementing the iterator protocol, not being a member of a particular class -- an iterator is as an iterator does. You can write your own custom classes that are iterators, and they won't be any of those classes you list.

From the point of view of "being an iterator", there is no difference between them. They are all iterators, and that just means you can iterate over them. There are might of course be other differences between them -- they might have additional methods or behavior defined -- but as iterators qua iterators they are the same.

You can view an iterator as some kind of doodad that "knows how" to iterate over a particular data structure. Different kinds of data structures might have their own custom classes for iterating over them; these iterators may do different things under the hood, but all share the same public interface (the iterator protocol).

like image 36
BrenBarn Avatar answered Sep 22 '22 15:09

BrenBarn