[错误处理]AttributeError: 'generator' object has no attribute 'next'

在python3下学习yield用法。

程序如下:

def bar(n):
    m = n 
    while True:
        m += 1
        yield m 
b = bar(3)
print(b.next())

程序报错

'generator' object has no attribute 'next'

错误原因

python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法。

原文地址:https://www.cnblogs.com/everfight/p/python_next.html