In the second example of zrange, there is an iter function in the zrange_iter class. I don’t understand why we need that function.
Iter function in iter class?
hey @mohitb ,
It was basically used to explain the difference between iterators and iterables .
As you see range is consumable only once whereas z_range can be consumed multiple times.
This is because you are generating a new iterator whenever we call the z_range object .
The __iter__
function helps us to iterate over this new iterator generator.
I hope this would helped you understand the main task.
Thank You .
I understand that the z_range can be consumed multiple times because the iter method in the z_range class returns a new zrange_iter object each time it is called but why is there a need for a iter method inside the zrange_iter class. Why would we need to call the iter method for the iterator class itself ? Wouldn’t just having the next method in the iterator class work?
The __iter__
function of zrange_iter class will be only be used whenever you call this class separately from the other class.
You are absolutely correct that the __next__
function completes our work.