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'的更多相关文章

  1. Python错误:AttributeError: 'generator' object has no attribute 'next'解决办法

    今天在学习生成器对象(generation object)运行以下代码时,遇到了一个错误: #定义生成器函数def liebiao(): for x in range(10): yield x#函数调 ...

  2. [错误处理]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()) 程序 ...

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

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

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

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

  5. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

  6. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  7. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  8. AttributeError: 'dict_values' object has no attribute 'translate'

    /***************************************************************************************** * Attribu ...

  9. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

随机推荐

  1. 总结这几天js的学习内容

    对js中难点的理解 1.把变量对象像遍历数组一样简单 对于数组 ,迭代出来的是数组元素,对于对象 ,迭代出来的是对象的属性: var obj = { w: "wen", j: &q ...

  2. 关于read和fread

    1.fread与read的区别---open和fopen的区别--fread函数和fwrite函数:http://blog.csdn.net/dreamtdp/article/details/7560 ...

  3. Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载、STelnet登录、SFTP登录

    >Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载.STelnet登录.SFTP登录 >>实验开始,参考<Huawei-R&S-网络工程 ...

  4. Jzzhu and Numbers

    Jzzhu and Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. 使用MySQLMigrationToolkit快速将Oracle数据导入MySQL

    使用MySQL Migration Toolkit快速将Oracle数据导入MySQL 上来先说点废话 本人最近在学习一些数据库方面的知识,之前接触过Oracle和MySQL,最近又很流行MongoD ...

  6. [POJ1733]Parity game(并查集 + 离散化)

    传送门 题意:有一个长度已知的01串,给出[l,r]这个区间中的1是奇数个还是偶数个,给出一系列语句问前几个是正确的 思路:如果我们知道[1,2][3,4][5,6]区间的信息,我们可以求出[1,6] ...

  7. hdu 4950

    #include<stdio.h> int main(){ __int64 h,a,b,k,j=0; while(scanf("%I64d%I64d%I64d%I64d" ...

  8. 某种密码(password.*)

    关于某种密码有如下描述:某种密码的原文A是由N个数字组成,而密文B是一个长度为N的01数串,原文和密文的关联在于一个钥匙码KEY.若KEY=∑▒[Ai*Bi],则密文就是原文的一组合法密码.现在有原文 ...

  9. nyoj_524_A-B Problem_201312012035

    A-B Problem 时间限制:1000 ms  |           内存限制:65535 KB 难度:3   描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. ...

  10. nyoj_8_一种排序_201311251238

    一种排序 时间限制:3000 ms  |           内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编 ...