Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法
今天在学习生成器对象(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'解决办法相关推荐
- python 错误AttributeError: ‘dict_keys‘ object has no attribute ‘sort‘
当运行Python代码时,出现以下错误: AttributeError: 'dict_keys' object has no attribute 'sort' 当代码有sort函数时,检查其他代码都没 ...
- Python错误:AttributeError: 'Worksheet' object has no attribute 'get_highest_row' 解决办法
今天开始学习<python编程快速上手>中的第12章:处理Excel电子表格.但是遇到了点小问题. 当我按照书中的方法获取最大行和最大列的时候,出现了下面的错误提醒: import ope ...
- 使用face_recognition模块:错误AttributeError:'PngStream' object has no attribute 'chunk fcWp'
使用face_recognition模块:错误AttributeError:'PngStream'object has no attribute 'chunk fcWp' 文章目录: 一.错误:Att ...
- 求助!spyder beautifulsoup4显示错误:AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initia
spyder beautifulsoup4显示错误: AttributeError: 'HTMLParserTreeBuilder' object has no attribute 'initia 实 ...
- pytorch 错误 AttributeError: ‘Tensor‘ object has no attribute ‘backword‘
pytorch 错误 AttributeError: 'Tensor' object has no attribute 'backword' 按错误说明是没有backword属性. 错误代码 loss ...
- 【Python】AttributeError: ‘DatetimeProperties’ object has no attribute ‘weekday_name’ 的解决方法
运行下面的代码: import pandas as pd# 创建日期 dates = pd.Series(pd.date_range("2/2/2002", periods=3, ...
- AttributeError: 'NoneType' object has no attribute 'sc' 解决方法(二)
上一次本以为可以解决了这个问题,然而并没有那么地简单.博主最近在edx网站学习pyspark,想打一下视频上的代码,结果报错了,依旧是报了"AttributeError:'NoneType' ...
- Python 中 AttributeError: ‘NoneType‘ object has no attribute ‘X‘ 错误
文章目录 AttributeError: 'NoneType' object has no attribute 'X' 介绍 AttributeError: 'NoneType' object has ...
- python pandas AttributeError: ‘DataFrame‘ object has no attribute ‘ix‘
python使用pandas DataFrame.ix的时候 AttributeError: 'DataFrame' object has no attribute 'ix' 在使用进行DataFra ...
最新文章
- k8s nginx ingress配置TLS
- 在Editplus中搭建Ruby开发环境
- ZAM 3D入门教程(3):Viewport
- [原创]关于在VS解决方案下使用文件夹管理多个项目层次关系的说明
- 第一批 90 后还有半个月就 30 岁了!那些 30 岁前就成博导的人都怎样了...
- Java并发编程之并发容器ConcurrentHashMap(JDK1.7)解析
- 简述计算机图形的图形应用主要有哪些,5计算机图形学考试简答题复习.doc
- Visual Studio 2012资源管理器里单击打开改为双击打开文件
- 根据html元素的类型设置元素的值
- [转]perl里使用外部命令
- python headers 随机_0基础学Python之反爬虫利器,搭建HTTP代理~~
- 最新楚琳web打印控件下载 - 2019适配所有浏览器
- 计算机怎么删除表格,EXCEL如何删除表格内容中的部分文本
- 软件学报zotero文献格式
- UVALive 6657
- java.lang.IllegalStateException: Migration didn‘t properly handle
- php直播推流rtmp,直播推流nginx-rtmp-module集成
- php 计算函数 相加,比较,相除,相减,求余,相乘
- CentOS7挂载NTFS分区步骤
- linux下使用mail定时发送邮件-阿里企业邮箱发送