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:
callable-iterator
? You definitely cannot call it.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.
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.
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.
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.
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]
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With