今天在学习生成器对象(generation object)运行以下代码时,遇到了一个错误:

#定义生成器函数
def liebiao():
for x in range(10):
yield x
#函数调用
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后发现,在python3.x版本中,python2.x的g.next()函数已经更名为g.__next__(),所以只需要将g.next()换成g.__next__()就可以了。如果你觉得g.__next__()太丑,使用next(g)也能达到相同效果。

这其实是版本更新所带来的无法避免的错误,毕竟python不像其他的编程语言,python2和python3之间互不兼容。
---------------------
作者:向东的笔记本
来源:CSDN
原文:https://blog.csdn.net/gaifuxi9518/article/details/81059938
版权声明:本文为博主原创文章,转载请附上博文链接!

Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法的更多相关文章

  1. python 错误AttributeError: 'module' object has no attribute 'AF_INET'

    写了一个简单的python socket的程序.运行时,报错如下 原因:文件的命名与Python的function的命名冲突 修改名称后,发现还是无法运行,检查目录下面是否有 这样子的一个文件,删除即 ...

  2. AttributeError: 'ForeignKey' object has no attribute 're' 解决办法

    使用 field_object.rel.model.objects.filter(**db_condition) 报错 forekey中存在rel,为什么不能调用? 通过以下语句观察 print(fi ...

  3. mygenerator().next() AttributeError: 'generator' object has no attribute 'next'

    def mygenerator(): print ("start ...") yield 5 mygenerator() print ("mygenerator():&q ...

  4. [错误处理]AttributeError: 'generator' object has no attribute 'next'

    在python3下学习yield用法. 程序如下: def bar(n): m = n while True: m += 1 yield m b = bar(3) print(b.next()) 程序 ...

  5. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  6. python提示AttributeError: 'NoneType' object has no attribute 'append'【转发】

    在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.appe ...

  7. python提示AttributeError: 'NoneType' object has no attribute 'append'

    在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.appe ...

  8. 提示AttributeError: 'module' object has no attribute 'HTTPSHandler'解决方法

    今天在新机器上安装sqlmap,运行提示AttributeError: 'module' object has no attribute 'HTTPSHandler' 网上找了找资料,发现一篇文章ht ...

  9. python3.6:AttributeError: 'generator' object has no attribute 'next'

    环境:PyCharm+Anaconda python版本:3.6 协程测试: #!/usr/bin/env python # -*- coding:utf-8 -*- import time def ...

随机推荐

  1. 2019.8.5 NOIP模拟测试13 反思总结【已更新完毕】

    还没改完题,先留个坑. 放一下AC了的代码,其他东西之后说… 改完了 快下课了先扔代码 跑了跑了 思路慢慢写 来补完了[x 刚刚才发现自己打错了标题 这次考试挺爆炸的XD除了T3老老实实打暴力拿了52 ...

  2. iOS播放器横竖屏切换

    http://www.cocoachina.com/cms/wap.php?action=article&id=20292 http://feihu.me/blog/2015/how-to-h ...

  3. 用 Python 写一个 NoSQL 数据库Python

    NoSQL 这个词在近些年正变得随处可见. 但是到底 “NoSQL” 指的是什么? 它是如何并且为什么这么有用? 在本文, 我们将会通过纯 Python (我比较喜欢叫它, “轻结构化的伪代码”) 写 ...

  4. vue+ElementUI项目中,input只能输入正整数的验证

    代码如下:     <el-input  v-model="famount" placeholder="请输入内容"   @keyup.native=&q ...

  5. Jquery选择器分类:基本选择器,层次选择器,过滤选择器,表单选择器。

    基本选择器 说明:通过元素id.class和标签名等来查找DOM元素 1.id选择器:$("#test");//选取id为test的元素 2.类选择器:$(".test& ...

  6. hdfs 的存储空间扩展

    问题:之前集群上每个节点的的大小为50G, 但是硬盘容量是160G的, 不明白为什么才50个G: 后来发现是因为dfs.data.dir设置的问题,该目录下挂载的磁盘空间的大小就会作为该节点的容量.. ...

  7. ecshop二次开发之百度地图

    案例效果展示: 代码实现: 1.在ecshop后台找到文章管理->文章分类->添加文章分类,添加一个顶级分类,叫做"合作单位",并且让其显示在导航栏.如下图: 1.在e ...

  8. 【JZOJ4161】于神之怒 莫比乌斯反演

    任务 答案mod 1e9+7. 解法 容易写出反演: Ans=∑T=1nTk∗∑i=1⌊nT⌋⌊niT⌋⌊miT⌋μ(i) ∑⌊nT⌋i=1⌊niT⌋⌊miT⌋μ(i)这个因式显然是经典的分块处理: ...

  9. 【JZOJ3852】【NOIP2014八校联考第2场第2试9.28】单词接龙(words)

    DDD Bsny从字典挑出N个单词,并设计了接龙游戏,只要一个单词的最后两个字母和另一个单词的前两个字母相同,那么这两个单词就可以有序的连接起来. Bsny想要知道在所给的所有单词中能否按照上述方式接 ...

  10. 【C++】位运算实现加减乘除

    #include<iostream> #include<assert.h> using namespace std; // 位运算实现加减乘除 int myAdd(int nu ...