[Tutor] Generator next()
Steven D'Aprano
steve at pearwood.info
Sat Dec 28 01:09:02 CET 2013
On Fri, Dec 27, 2013 at 07:16:22PM +0000, Oscar Benjamin wrote:
> The next method of iterators was renamed __next__ in Python 3. So if you
> change it to self.col.__next__() it will work in Python 3. Neither of those
> is really correct though. The correct method is to call the next built-in:
> next(self.col)
> That will work under Python 2 and 3 and is the recommended way in both
> versions.
Not quite. There is no next() function prior to Python 2.6. In 2.5 you
have to call self.col.next(), which was deemed to be a mistake and
changed in 2.6.
A general word of advice: never call "dunder" (Double UNDERscore)
methods like __next__ directly. There are exceptions to that rule, but
it generally applies.
--
Steven
More information about the Tutor
mailing list