Is generator.next() visible in python 3.0?

2019-01-04 19:04发布

I have a generator that generates a series, for example:

def triangleNums():
    '''generate series of triangle numbers'''
    tn = 0
    counter = 1
    while(True):
        tn = tn + counter
        yield tn
        counter = counter + 1

in python 2.6 I am able to make the following calls:

g = triangleNums() # get the generator
g.next()           # get next val

however in 3.0 if I execute the same two lines of code I'm getting the following error:

AttributeError: 'generator' object has no attribute 'next'

but, the loop iterator syntax does work in 3.0

for n in triangleNums():
    if not exitCond:
       doSomething...

I've not been able to find anything yet that explains this difference in behavior for 3.0.

3条回答
等我变得足够好
2楼-- · 2019-01-04 19:28

Try:

next(g)

Check out this neat table that shows the differences in syntax between 2 and 3 when it comes to this.

查看更多
混吃等死
3楼-- · 2019-01-04 19:31

Correct, g.next() has been renamed to g.__next__(). The reason for this is consistency: Special methods like __init__() and __del__ all have double underscores (or "dunder" in the current vernacular), and .next() was one of the few exceptions to that rule. This was fixed in Python 3.0. [*]

But instead of calling g.__next__(), as Paolo says, use next(g).

[*] There are other special attributes that have gotten this fix; func_name, is now __name__, etc.

查看更多
欢心
4楼-- · 2019-01-04 19:38

If your code must run under Python2 and Python3, use the 2to3 six library like this:

import six

six.next(g)  # on PY2K: 'g.next()' and onPY3K: 'next(g)'
查看更多
登录 后发表回答