Iterators and Iterables

By : Akshar Raaj

Iterable

Iterables are objects that implement the iterator protocol.

Iterator protocol mandates that __iter__ method be implemented on the object.

class A(object):
    def __iter__(self):
        return B()

An instance of A would be an iterable, because class A has __iter__() defined on it.

__iter__ method mandates that an iterator be returned from it. Instance of class B must be an iterator. More on iterators to follow. iterator and iterables are different things.

a = A()

Here "a" is an iterable. It is not an iterator.

There is a built-in method called iter(). Only iterables can be passed to built in ...

more info..
Topics : python
© Agiliq, 2009-2012