python 3.x 错误 ‘generator’ object has no attribute ‘next’
2017-06-20 09:51
453 查看
用python生成斐波那契數列
运行正常
当运行下面的方式时
f=fab(5)
f.next()
出现下面的错误
Traceback (most recent call last):
File “<pyshell#32>”, line 1, in <module>
f.next()
AttributeError: ‘generator’ object has no attribute ‘next’
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
修改为下面这样运行正常
f=fab(5)
f.__next__()
def fab(max): n,a,b=0,0,1 while n<max: yield b a,b=b,a+b n=n+1 for n in fab(5): print(n)
运行正常
当运行下面的方式时
f=fab(5)
f.next()
出现下面的错误
Traceback (most recent call last):
File “<pyshell#32>”, line 1, in <module>
f.next()
AttributeError: ‘generator’ object has no attribute ‘next’
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
修改为下面这样运行正常
f=fab(5)
f.__next__()
相关文章推荐
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- Python3.6 下yield的常见错误(AttributeError: 'generator' object has no attribute 'next')
- python 3.2 错误 ‘generator’ object has no attribute ‘next’
- python机器学习实战3.2提示错误AttributeError: type object 'str' has no attribute '_name_'
- Python2.X与Python3.X编程代码笔记(1):AttributeError: 'dict' object has no attribute 'iteritems'
- python 2.x和3.x关于 'module' object has no attribute 'open'的解决办法
- [python错误]builtins.AttributeError: 'module' object has no attribute 'request'
- Python3.X出现AttributeError: module 'urllib' has no attribute 'urlopen'错误
- 【Python错误】AttributeError: 'bool' object has no attribute '__call__'
- Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
- Python错误AttributeError: 'module' object has no attribute 'copy'
- python 错误解析 AttributeError: 'module' object has no attribute 'dumps'
- python “AttributeError: '_csv.reader' object has no attribute'next'” 报错的解决办法
- Python问题:AttributeError: '_csv.reader' object has no attribute 'next' 以及UnicodeDecodeError: 'gbk' co
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
- python 2.x和3.x关于 'module' object has no attribute 'open'的解决办法
- python 编译错误TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'