I didn’t get how zrange works, how those classes are iterators and iterables. Also how does iter function work.
Cant understand zrange class
hey @amoghjalan2005 ,
The difference between iterators and iterables is that
if a function is an iterator then its object returns a particular value on calling the __next__
function , means it will print values untill it reaches the last index and stops iterating.
Whereas if a function is an iterable then its object returns a iterator , which mean whenever a new object is created or a function call is made it return a new iterator.
Simplifying iterable is a class that can be iterated multiple times and iterator can iterated only once , until or unless you create a new object of that class.
__iter__
function converts a simple class into an iterator and that class should have some functions or inheritance which supports iteration.
IF you see the video , the __iter__
function makes a call to zrange_iter class which is an iterator class , hence you are able to iterate over it using loops. And to iterate over next values , it uses another magic function __next__
which just changes the index of values and returns that value.
I hope this would have helped you understand the topic.
Thank You and Happy Learning .