The Python forums and other question-and-answer websites like Quora and Stackoverflow are full of questions concerning 'iterators' and 'iterable'. Some want to know how they are defined and others want to know if there is an easy way to check, if an object is an iterator or an iterable. We will provide further down a function for this purpose.
We have seen that we can loop or iterate over various Python objects like lists, tuples and strings for example.
for city in ["Berlin", "Vienna", "Zurich"]:print(city)for city in ("Python", "Perl", "Ruby"):print(city)for char in "Iteration is easy":print(char)
BerlinViennaPythonZurich PerlaRuby I t e r t iso n i s e ay
This form of looping can be seen as iteration. Iteration is not restricted to explicit for loops. If you call the function sum, - e.g. on a list of integer values, - you do iteration as well.
So what is the difference between an iterable and an iterator?
In one perspective they are the same: You can iterate with a for loop over iterators and iterables. Every iterator is also an iterable, but not every iterable is an iterator. E.g. a list is iterable but a list is not an iterator! An iterator can be created from an iterable by using the function 'iter'. To make this possible the class of an object needs either a method '__iter__', which returns an iterator, or a '__getitem__' method with sequential indexes starting with 0.
Iterators are objects with a '__next__' method, which will be used when the function 'next' is called.
So what is going on behind the scenes, when a for loop is executed? The for statement calls iter() on the object ( which should be a so-called container object), which it is supposed to loop over. If this call is successful, the iter call will return return an iterator object that defines the method __next__() which accesses elements of the object one at a time. The __next__() method will raise a StopIteration exception, if there are no further elements available. The for loop whill terminate as soon as it catches a StopIteration exception. You can call the __next__() method using the next() built-in function. This is how it works:
cities = ["Berlin", "Vienna", "Zurich"]iterator_obj = iter(cities)print(iterator_obj)print(next(iterator_obj))print(next(iterator_obj))print(next(iterator_obj))
<list_iterator object at 0x7f08a055e0b8>Berlin ViennaZurich
If we called 'next(iterator_obj)' one more time, it would return 'StopIteration'
The following function 'iterable' will return True, if the object 'obj' is an iterable and False otherwise.
def iterable(obj):try:iter(obj)return Trueexcept TypeError:return Falsefor element in [34, [4, 5], (4, 5), {"a":4}, "dfsdf", 4.5]:print(element, "iterable: ", iterable(element))
34 iterable: False[4, 5] iterable: True(4, 5) iterable: True{'a': 4} iterable: Truedfsdf iterable: True4.5 iterable: False
We have described how an iterator works. So if you want to add an iterator behavior to your class, you have to add the __iter__ and the __next__ method to your class. The __iter__ method returns an iterator object. If the class contains a __next__, it is enough for the __iter__ method to return self, i.e. a reference to itself:
class Reverse:"""Creates Iterators for looping over a sequence backwards."""def __init__(self, data):self.data = dataself.index = len(data)def __iter__(self):return selfdef __next__(self):if self.index == 0:raise StopIterationself.index = self.index - 1return self.data[self.index]lst = [34, 978, 42]lst_backwards = Reverse(lst)for el in lst_backwards:print(el)
4297834
No comments:
Post a Comment