python運行錯誤及解決——AttributeError: 'generator' object has no attribute '_next_'

>>> a=(x*3 for x in range(5))
>>> a._next_()
Traceback (most recent call last):
  File "<stdin>", 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章