Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法
今天在學(xué)習(xí)生成器對象(generation object)運行以下代碼時,遇到了一個錯誤:
#定義生成器函數(shù)
def liebiao():
for x in range(10):
yield x
#函數(shù)調(diào)用
g = liebiao()
#打印元素
print(g.next())
D:\>python test.py
Traceback (most recent call last):
File "test.py", line 10, in <module>
print(g.next())
AttributeError: 'generator' object has no attribute 'next'
Google后發(fā)現(xiàn),在python3.x版本中,python2.x的g.next()函數(shù)已經(jīng)更名為g.__next__(),所以只需要將g.next()換成g.__next__()就可以了。如果你覺得g.__next__()太丑,使用next(g)也能達(dá)到相同效果。
這其實是版本更新所帶來的無法避免的錯誤,畢竟python不像其他的編程語言,python2和python3之間互不兼容。
---------------------
作者:向東的筆記本
來源:CSDN
原文:https://blog.csdn.net/gaifuxi9518/article/details/81059938
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!
總結(jié)
以上是生活随笔為你收集整理的Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 代码耦合
- 下一篇: python特性(八):生成器对象的se