Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes something iterable in python

Tags:

python

What makes something iterable in Python? ie. can loop over it with for

Is it possible for me to create an iterable class in Python? If so, how?

like image 363
zudduz Avatar asked Mar 10 '11 15:03

zudduz


People also ask

How do you know if its iterable?

As of Python 3.4, the most accurate way to check whether an object x is iterable is to call iter(x) and handle a TypeError exception if it isn't. This is more accurate than using isinstance(x, abc. Iterable) , because iter(x) also considers the legacy __getitem__ method, while the Iterable ABC does not.

What is iterable and iterable in Python?

An Iterable is basically an object that any user can iterate over. An Iterator is also an object that helps a user in iterating over another object (that is iterable).

What types are iterable in Python?

Examples of iterables include all sequence types (such as list , str , and tuple ) and some non-sequence types like dict , file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements Sequence semantics.


2 Answers

To make a class iterable, write an __iter__() method that returns an iterator:

class MyList(object):     def __init__(self):         self.list = [42, 3.1415, "Hello World!"]     def __iter__(self):         return iter(self.list)  m = MyList() for x in m:     print(x) 

prints

42 3.1415 Hello World! 

The example uses a list iterator, but you could also write your own iterator by either making __iter__() a generator or by returning an instance of an iterator class that defines a __next__() method.

like image 162
Sven Marnach Avatar answered Oct 01 '22 09:10

Sven Marnach


The Python documentation describes exactly this:

One method needs to be defined for container objects to provide iteration support:

container.__iter__() 

Return an iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. (An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.) This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

The iterator objects themselves are required to support the following two methods, which together form the iterator protocol:

iterator.__iter__() 

Return the iterator object itself. This is required to allow both containers and iterators to be used with the for and in statements. This method corresponds to the tp_iter slot of the type structure for Python objects in the Python/C API.

iterator.next() 

Return the next item from the container. If there are no further items, raise the StopIteration exception. This method corresponds to the tp_iternext slot of the type structure for Python objects in the Python/C API.

like image 35
Nate Avatar answered Oct 01 '22 09:10

Nate