今天在学习生成器对象(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: ‘dict_keys‘ object has no attribute ‘sort‘

    当运行Python代码时,出现以下错误: AttributeError: 'dict_keys' object has no attribute 'sort' 当代码有sort函数时,检查其他代码都没 ...

  2. Python错误:AttributeError: 'Worksheet' object has no attribute 'get_highest_row' 解决办法

    今天开始学习<python编程快速上手>中的第12章:处理Excel电子表格.但是遇到了点小问题. 当我按照书中的方法获取最大行和最大列的时候,出现了下面的错误提醒: import ope ...

  3. 使用face_recognition模块:错误AttributeError:'PngStream' object has no attribute 'chunk fcWp'

    使用face_recognition模块:错误AttributeError:'PngStream'object has no attribute 'chunk fcWp' 文章目录: 一.错误:Att ...

  4. 求助!spyder beautifulsoup4显示错误:AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initia

    spyder beautifulsoup4显示错误: AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initia 实 ...

  5. pytorch 错误 AttributeError: ‘Tensor‘ object has no attribute ‘backword‘

    pytorch 错误 AttributeError: 'Tensor' object has no attribute 'backword' 按错误说明是没有backword属性. 错误代码 loss ...

  6. 【Python】AttributeError: ‘DatetimeProperties’ object has no attribute ‘weekday_name’ 的解决方法

    运行下面的代码: import pandas as pd# 创建日期 dates = pd.Series(pd.date_range("2/2/2002", periods=3, ...

  7. AttributeError: 'NoneType' object has no attribute 'sc' 解决方法(二)

    上一次本以为可以解决了这个问题,然而并没有那么地简单.博主最近在edx网站学习pyspark,想打一下视频上的代码,结果报错了,依旧是报了"AttributeError:'NoneType' ...

  8. Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 错误

    文章目录 AttributeError: 'NoneType' object has no attribute 'X' 介绍 AttributeError: 'NoneType' object has ...

  9. python pandas AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘

    python使用pandas DataFrame.ix的时候 AttributeError: 'DataFrame' object has no attribute 'ix' 在使用进行DataFra ...

最新文章

  1. k8s nginx ingress配置TLS
  2. 在Editplus中搭建Ruby开发环境
  3. ZAM 3D入门教程(3):Viewport
  4. [原创]关于在VS解决方案下使用文件夹管理多个项目层次关系的说明
  5. 第一批 90 后还有半个月就 30 岁了!那些 30 岁前就成博导的人都怎样了...
  6. Java并发编程之并发容器ConcurrentHashMap(JDK1.7)解析
  7. 简述计算机图形的图形应用主要有哪些,5计算机图形学考试简答题复习.doc
  8. Visual Studio 2012资源管理器里单击打开改为双击打开文件
  9. 根据html元素的类型设置元素的值
  10. [转]perl里使用外部命令
  11. python headers 随机_0基础学Python之反爬虫利器,搭建HTTP代理~~
  12. 最新楚琳web打印控件下载 - 2019适配所有浏览器
  13. 计算机怎么删除表格,EXCEL如何删除表格内容中的部分文本
  14. 软件学报zotero文献格式
  15. UVALive 6657
  16. java.lang.IllegalStateException: Migration didn‘t properly handle
  17. php直播推流rtmp,直播推流nginx-rtmp-module集成
  18. php 计算函数 相加,比较,相除,相减,求余,相乘
  19. CentOS7挂载NTFS分区步骤
  20. linux下使用mail定时发送邮件-阿里企业邮箱发送

热门文章

  1. 2022-2028年中国特高压电网行业深度调研及投资前景预测报告
  2. 2022-2028年中国塑料人造革行业市场研究及前瞻分析报告
  3. 2022-2028年中国塑料导爆管行业市场调查研究及前瞻分析报告
  4. Redis 高级特性(4)— 单线程架构
  5. Qt---布局,设置控件边距,拉伸因子
  6. C++核心编程(四)--文件操作
  7. CMD 输入中文看不到输入法的解决方法
  8. 正则表达式中的\.表示什么意思
  9. LeetCode简单题之到目标元素的最小距离
  10. 从PyTorch到ONNX的端到端AlexNet