python运行错误及解决——AttributeError: 'generator' object has no attribute '_next_'

>>> a=(x*3 for x in range(5))
>>> a._next_()
Traceback (most recent call last):
  File "", line 1, in <module>
AttributeError: 'generator' object has no attribute '_next_'

运行时出现上述错误,以为是python版本问题,结果发现:
next前后的"_"是两个连用

错误: _next_()
正确:__next__()

>>> a=(x*3 for x in range(5))
>>> a.__next__()
0
>>> a.__next__()
3

ok

你可能感兴趣的:(python运行错误及解决——AttributeError: 'generator' object has no attribute '_next_')