0

I was reading a book about python (beginning python, from novice to prefessional, 3rd edition) and noticed the following sentence which has confused me considering that I am a beginner

"in many cases you would put the iter method in another object which you would use in the for loop, that would then return your iterator" page 179

I read every sources that I could find and I am still confused because in all of them we put the iter in one class, make objects from it and then that object gets used in for loop and we never put it in another class

I would be grateful if you correct me if i am wrong and give me a simple example if this could also be done

  • The `__iter__` method should return a class with a `__next__` method, or a generator. The advantage of using a separate class is it allows you to iterate over a object multiple times without consuming it. For example `iter([])` returns a `list_iterator`, not a list. You can create multiple iterators from the same list – mousetail May 15 '23 at 06:36

0 Answers0