What are the methods of the iteration class next () and __next __ () for, and what is the difference?

I get a little lost on python iterators. Sometimes I use them, but I don’t remember being created. I read 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 assume that the method __iter__()should return an iterator, and that the iterator should have the following method correctly? Then what about the method __next__()? is it for direct iteration over a class if it does not return another iterator using the method __iter__()?

+5
source share
2 answers

PEP 3114 renamed iterator.next()in iterator.__next__(). This was implemented in version 3.0. The link above contains all the gory details.

+7

next __next__ Python 3. , StopIteration, .

+4

All Articles