首页 > 编程语言 >python 2.x 错误 ‘generator’ object has no attribute ‘_F_hawk189_新浪博客

python 2.x 错误 ‘generator’ object has no attribute ‘_F_hawk189_新浪博客

时间:2022-11-02 17:04:16浏览次数:48  
标签:__ generator no python next ___ 用法

斐波那契数列:

def f():

a,b=1,1

while True:

yield a

a,b=b,a+b



a=f()

for i in range(10):

print(a.___next___(),' ')


AttributeError: 'generator' object has no attribute '___next___'
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next才是python 2.x的用法
2.x用法:
a.next()
3.x用法:
a.__next()__

标签:__,generator,no,python,next,___,用法
From: https://blog.51cto.com/u_15858333/5817789

相关文章