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: 'module' object has no attribute 'AF_INET'
写了一个简单的python socket的程序.运行时,报错如下 原因:文件的命名与Python的function的命名冲突 修改名称后,发现还是无法运行,检查目录下面是否有 这样子的一个文件,删除即 ...
- AttributeError: 'ForeignKey' object has no attribute 're' 解决办法
使用 field_object.rel.model.objects.filter(**db_condition) 报错 forekey中存在rel,为什么不能调用? 通过以下语句观察 print(fi ...
- mygenerator().next() AttributeError: 'generator' object has no attribute 'next'
def mygenerator(): print ("start ...") yield 5 mygenerator() print ("mygenerator():&q ...
- [错误处理]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()) 程序 ...
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...
- 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 ...
- 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 ...
- 提示AttributeError: 'module' object has no attribute 'HTTPSHandler'解决方法
今天在新机器上安装sqlmap,运行提示AttributeError: 'module' object has no attribute 'HTTPSHandler' 网上找了找资料,发现一篇文章ht ...
- python3.6:AttributeError: 'generator' object has no attribute 'next'
环境:PyCharm+Anaconda python版本:3.6 协程测试: #!/usr/bin/env python # -*- coding:utf-8 -*- import time def ...
随机推荐
- [jnhs]id字段修改错误导致hibernate hql查询整表只返回第一条数据
调试发现,查询到的就是一条数据 hql语句执行结果 Hibernate: select ballmodel0_.ball_id as ball_id1_1_, ballmodel0_.color as ...
- easyui combobox下拉框中显示大于号小于号的问题
前两天同事做了个功能,通过勾选下拉框里的值进行列表查询,结果下拉框里的值是“0<t<=2”.“2<t<=5”.“t>5”这样的. combobox是用脚本渲染出来的,里面 ...
- Expires
(装载) 简要:添加Expires头能有效的利用浏览器的缓存能力来改善页面的性能,能在后续的页面中有效避免很多不必要的Http请求,WEB服务器使用Expires头来告诉Web客户端它可以使用一个组件 ...
- dns-prefetch对网站速度能提升有多少?详解dns-prefetch。
DNS解析场景 有很多大型的网站,都会用N 个CDN 域名来做图片.静态文件等资源访问.比如新浪,我们经常会看到有下列域. img1.sina.com.cn . img2.sina.com.cn .i ...
- NodeJS概述2-事件插件-简易爬虫
事件 events 模块 原生事件写法 /* * 1. 事件分类 * DOM0级 事件 - on + eventType * DOM2级 事件 - 事件监听 * 2. 事件构成部分有哪些? dom.o ...
- Ajax.dll使用方法和步骤(比较方便的实现ajax)
转载自 博客频道 - CSDN.NET http://blog.csdn.net/houhanxin1/article/details/6671470 1. 有直接用框架的:Ajaxpro和Aja ...
- 杨柳絮-Info:对抗杨柳絮的7种方法和2种防治手段
ylbtech-杨柳絮-Info:对抗杨柳絮的7种方法和2种防治手段 园林养护人员在对抗杨柳絮上 主要有以下两种方法↓↓ 1.化学方法 化学方法是通过激素等调节剂来抑制植物发芽分化,达到减少杨柳开花的 ...
- Ubuntu的网络共享
实际场景 公司项目中遇到一个场景:Ubuntu的主机上装了个4G卡(USB模式),需要将这个4G网共享给一个AP,使得所有连接AP的移动设备都可以通过4G上外网 方法很简单: 1. 将4G网口之外的另 ...
- python findall函数
- 一.数据结构&算法的引言+时间复杂度
目录(contents): 1.什么是计算机科学?什么是算法? 2.如何形象化的理解算法? 3.什么是算法分析? 4.时间复杂度 5.数据结构 6.总结算法和数据结构之间的关联 一.什么是计算机科学? ...