A question about class as an iterator

Terry Reedy tjreedy at udel.edu
Sun Aug 28 23:26:11 CEST 2011


On 8/28/2011 1:22 PM, Yaşar Arabacı wrote:

> I got confused about classes as an iterator. I saw something like this:
>
> class foo():
>      __iter__(self):
>          return self
>      next(self):
>          return something

2.x

> But then I saw a __next__ method on some code.

3.x
This might work in 2.6 or .7, but I do not really know.

The next() builting was introduced in 2.6 so that *users* could write
item = next(someiter)
and have that work in 2.6+ and in 3.x without change.

> which one should I use?

Depends on your Python version. I recommend you use 3.2 unless you have 
a reason or need to use something else.

-- 
Terry Jan Reedy





More information about the Python-list mailing list