I am a little bit lost on python iterators. I occasionally use them, but I don't remember creating one myself. I read from somewhere that I don't remember where, a code like this:
class Foo(object):
def __init__(self):
self.something = "initial_value"
def __iter__(self):
return self
def next(self):
# I don't quite remember what was here :S
return self.something
I guess __iter__()
method supposed to return an iterator, and that iterator should have a next method right? Then what about __next__()
method? is it for directly iterating over a class without it returning another iterator with __iter__()
method?
PEP 3114 renamed iterator.next()
to iterator.__next__()
. This was implemented in version 3.0. The link above contains all the gory details.
next
has been renamed to __next__
in Python 3. As for what it does, it should return the next item, or raise StopIteration
if there are no more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With