Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.
In Python, iterable and iterator have specific meanings.
An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.
An iterator is an object with a next (Python 2) or __next__ (Python 3) method.
Whenever you use a for loop, or map, or a list comprehension, etc. in Python, the next method is called automatically to get each item from the iterator, thus going through the process of iteration.
A good place to start learning would be the iterators section of the tutorial and the iterator types section of the standard types page. After you understand the basics, try the iterators section of the Functional Programming HOWTO.
Answer from agf on Stack OverflowIteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.
In Python, iterable and iterator have specific meanings.
An iterable is an object that has an __iter__ method which returns an iterator, or which defines a __getitem__ method that can take sequential indexes starting from zero (and raises an IndexError when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.
An iterator is an object with a next (Python 2) or __next__ (Python 3) method.
Whenever you use a for loop, or map, or a list comprehension, etc. in Python, the next method is called automatically to get each item from the iterator, thus going through the process of iteration.
A good place to start learning would be the iterators section of the tutorial and the iterator types section of the standard types page. After you understand the basics, try the iterators section of the Functional Programming HOWTO.
Here's the explanation I use in teaching Python classes:
An ITERABLE is:
- anything that can be looped over (i.e. you can loop over a string or file) or
- anything that can appear on the right-side of a for-loop:
for x in iterable: ...or - anything you can call with
iter()that will return an ITERATOR:iter(obj)or - an object that defines
__iter__that returns a fresh ITERATOR, or it may have a__getitem__method suitable for indexed lookup.
An ITERATOR is an object:
- with state that remembers where it is during iteration,
- with a
__next__method that:- returns the next value in the iteration
- updates the state to point at the next value
- signals when it is done by raising
StopIteration
- and that is self-iterable (meaning that it has an
__iter__method that returnsself).
Notes:
- The
__next__method in Python 3 is speltnextin Python 2, and - The builtin function
next()calls that method on the object passed to it.
For example:
>>> s = 'cat' # s is an ITERABLE
# s is a str object that is immutable
# s has no state
# s has a __getitem__() method
>>> t = iter(s) # t is an ITERATOR
# t has state (it starts by pointing at the "c"
# t has a next() method and an __iter__() method
>>> next(t) # the next() function returns the next value and advances the state
'c'
>>> next(t) # the next() function returns the next value and advances
'a'
>>> next(t) # the next() function returns the next value and advances
't'
>>> next(t) # next() raises StopIteration to signal that iteration is complete
Traceback (most recent call last):
...
StopIteration
>>> iter(t) is t # the iterator is self-iterable
I'm somewhat confused about difference between Iterator and Iterable
What is iterators and generators?
iterator and iterable object, is there a difference?
Am I the only one who thinks channels are the obvious choice for iterators?
My book says "any object with a .next() method is called an iterator, which returns the next value in the range and raises the StopIt exception upon reaching the end".
However, range() function has no next() method, and is however, said to return an iterable object. Thus:
x = range(1, 10) x.__next__()
returns an error. But, the file iterators:
x = open("file.txt")
x.__next__()Doesn't return an error. Is there a difference between iterators and iterable objects?
so I know how to implement (e.g public Iterator<E> iterator() { return new ListIterator<E>(//could add first element here); }
but sometimes I see it saying public iterable. what's the difference? I think it is iterator is like you go through it once and you're done but iterable you get back a list that you can go through over and over again?
if it helps the thing I'm having trouble about is this: http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html
public Iterable<Board> neighbors()
do I just have to return a linked list of some sort (stack/queue)? why not use iterator<Board> instead?
Edit: I got it, you're returning an object that is iterable itself.
» npm install gpt-tokenizer