Alex Rivera | Logout

there's no next() function in a yield generator in python 3

Asked 2012-09-05T04:45:21.537
90

In this question, I have an endless sequence using Python generators. But the same code doesn't work in Python 3 because it seems there is no next() function. What is the equivalent for the 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())
Edit
Report

1 Answer

138

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

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

answered 2012-09-05T04:48:35.250

Your Answer