Hello my friends in this Entry we are going to talk about a really cool topic (Well that's what I think) that is Iterators and Generators with the Python Programming Language, and for explaining each of this I'm going to build a class representing a Linked List data structure and how you can use Python Built-in Functions to iterate, create iterators or generators on custom classes.
Accordinly to the Wikipedia, “a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence”.
And here I give you a class implementation of the Linked List already built with some basics helper methods, most of this code I took it from the blog article of Adnan Alam, he explains really well this class, though I made some changes from the original code.
Here we have two classes, a class for Node and a class for LinkedList, if you are not used to Object Oriented Programming in Python take a look at my two earlier entries in this topic.
Now as mentioned it before, this data structure could represent a sequence, so this could mean that we can index or iterate through it, well not yet, we need to provide to this class the methods that will allow us to perform these operations.
Set and Get Item
In our own data structures sometimes we want to have the capability to perform indexing access to the sequences, in same way we get and set elements in a Python List, we can achieved this by implementing __getitem__ and __setitem__ methods to our class, Python List are really versatile in this kind of operations, you can even slice using square brackets, but we are going to go that far, we are going to implement simple index access capable of doing even negatives, some validations must be done like only integers and values less or equal than the size of the sequence, let's build these methods.
Now let's make some tests on this
Pretty cool right? Now If we want to iterate through this linked list using a for loop, is not possible yet and that is because, so far this class is not an iterator.
we turn these objects into iterators if we build the __iter__ and next methods to our LinkedList class, these methods are used by the for loop for iteration, and the code is as follow.
As you can see if there is no more elements to iterate we need to raise a stop iteration error, this will indicate the for loop to stop, let's try this now.
Something to keep in mind is that for Python 3, next for classes is used with double underscore __next__; now that we have cover iterators let's talk about generators.
Generators are a kind of iterators, well they behave like iterators, but they are implemented as functions and instead of returning a value they yield a value, the main advantage of this is that they do not allocate all the values in memory rather calculated on the fly, that is why is called generators, you won't find too much differences in terms of performance until you find you self in need of processing really large data sets.
A really simple generator is the calculation of Fibonacci numbers; here I show you a simple code for this.
Now let's test this
Something to point out here is that generators as well iterators can only be iterated with next once until they reach the end, and they can be easily used with for loops, now let's say we want to convert our Linked List into a generator; we can build a generator method and yield each value at a time.
And finally let's test this
Well remember that this has meaning if there are too much elements in the LinkedList, so technics to process this information is required in order to improve execution performance.
We have come to the end of this entry; well I hope you have found this entry helpful to you. So thanks for coming by my new entry and I hope you have enjoyed this as much as I enjoyed writing it, stop by the comments if you want to discuss about this, share in your social media and subscribe. Cheers my friends.