There is no next () function in the output generator in python 3

In this question , I have an infinite sequence using Python generators. But the same code does not work in Python 3 because there seems to be no next() function. What is equivalent to next function?

 def updown(n): while True: for i in range(n): yield i for i in range(n - 2, 0, -1): yield i uptofive = updown(6) for i in range(20): print(uptofive.next()) 
+49
python
05 Sep '12 at 4:45
source share
2 answers

In Python 3, use next(uptofive) instead of uptofive.next() .

The built-in function next() also works in Python 2.6 or higher.

+74
Sep 05
source share

In Python 3, to make the syntax more consistent, the next() method was renamed to __next__() . You can use this one. This is explained in PEP 3114 .

Greg's next solution and calling the builtin next() function (which then tries to find the __next__() object method) is recommended.

+29
Sep 05
source share



All Articles