From reddit:
"hey in the iterator part, the iter function resets self.n and returns self. so, i didnt test it, but if i am not mistaken, this means if you get an iterator and iterate it a few times, then getting another iterator from the same object should reset self.n for the first iterator as well. this can lead to self.n exceeding the desired max. iteration for the first iterator since it gets reset at some point when the second iterator is created."
See both of the answers here
Mention all solutions:
- extraction of iterator to separate class
- return built-in iterator on relevant object data, if applicable
- make
__iter__ a generator function
This should probably also be added to Common Gotchas 😅
From reddit:
"hey in the iterator part, the iter function resets self.n and returns self. so, i didnt test it, but if i am not mistaken, this means if you get an iterator and iterate it a few times, then getting another iterator from the same object should reset self.n for the first iterator as well. this can lead to self.n exceeding the desired max. iteration for the first iterator since it gets reset at some point when the second iterator is created."
See both of the answers here
Mention all solutions:
__iter__a generator functionThis should probably also be added to Common Gotchas 😅