mygenerator().next() AttributeError: 'generator' object has no attribute 'next'
def mygenerator():
print ("start ...")
yield 5 mygenerator()
print ("mygenerator():",mygenerator())
mygenerator().next()
我定义了带有yield的函数,调用是报错:
mygenerator().next()
AttributeError: 'generator' object has no attribute 'next'
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
解决方法:把next()换为:_ _next_ _()
# -*- coding:utf-8 -*-
def mygenerator():
print ("start ...")
yield 5 print ("mygenerator():",mygenerator())
mygenerator().__next__()
mygenerator(): <generator object mygenerator at 0x0000000002324728>
start
mygenerator().next() AttributeError: 'generator' object has no attribute 'next'的更多相关文章
- Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法
		今天在学习生成器对象(generation object)运行以下代码时,遇到了一个错误: #定义生成器函数def liebiao(): for x in range(10): yield x#函数调 ... 
- [错误处理]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()) 程序 ... 
- python3.6:AttributeError: 'generator' object has no attribute 'next'
		环境:PyCharm+Anaconda python版本:3.6 协程测试: #!/usr/bin/env python # -*- coding:utf-8 -*- import time def ... 
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
		最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ... 
- AttributeError: 'list' object has no attribute 'write_pdf'
		我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ... 
- attributeError:'module' object  has no attribute ** 解决办法
		写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ... 
- AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'
		/*************************************************************************** * AttributeError: 'modu ... 
- AttributeError: 'dict_values' object has no attribute 'translate'
		/***************************************************************************************** * Attribu ... 
- python3  AttributeError: 'NoneType' object has no attribute 'split'
		from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ... 
随机推荐
- Netty 长连接服务
			转自:https://www.dozer.cc/2014/12/netty-long-connection.html 推送服务 还记得一年半前,做的一个项目需要用到 Android 推送服务.和 iO ... 
- linu学习第二天:文件系统相关操作
			1 ---第二天笔记--- 2 查看操作系统版本:cat /etc/redhat-release, /etc/os-release 3 命令:lsb_release 4 查看内存 和 swap分区:f ... 
- Python - 模块(一)
			目录 Python - 模块(一) 模块的引用方式 常用模块 random(随机模块) os模块 sys 序列化模块 hashlib subprocess optparse struct Python ... 
- 【Codeforces 126B】Password
			[链接] 我是链接,点我呀:) [题意] 给你一个字符串s 让你从中选出来一个字符串t 这个字符串t是s的前缀和后缀 且在除了前缀和后缀之外的中间部位出现过. 且要求t的长度最长. 让你输出这个字符串 ... 
- [bzoj3224]普通平衡树[Treap]
			Treap 的各种操作,模板题,要再写几遍 #include <iostream> #include <algorithm> #include <cstdio> # ... 
- HBase行键的设计
			rowkey是行的主键,而且hbase只能用rowkey范围即scan来查找数据.rowkey是以字典排序的.可以巧妙设计行键,比如想通过电影的评价进行排序,可以把评分rate和电影id组合起来,ra ... 
- hdu_1048_The Hardest Problem Ever_201311052052
			The Hardest Problem Ever Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ... 
- [bzoj2599][IOI2011]Race_树上点分治
			Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ... 
- FOJ 10月赛题 FOJ2198~2204
			A题. 发现是递推可以解决这道题,a[n]=6*a[n-1]-a[n-2].因为是求和,可以通过一个三维矩阵加速整个计算过程,主要是预处理出2^k时的矩阵,可以通过这道题 #include <i ... 
- Maven 的dependency 的 classifier的作用
			直接看一个例子,maven中要引入json包,于是使用了 <dependency> <groupId>net.sf.json-lib</groupId> <a ... 
