Python3.6 下yield的常见错误(AttributeError: 'generator' object has no attribute 'next')
来源:互联网 发布:手机理发软件 编辑:程序博客网 时间:2024/06/04 00:33
今天在python下写了协程的简单实现,但是竟然出现了错误,代码如下:
#-*- coding:utf-8 -*-import timedef A(): while True: print('----A----') yield time.sleep(0.5)def B(c): print('----B----') c.next() time.sleep(0.5)if __name__ == '__main__': a = A() B(a)
报错:
AttributeError: ‘generator’ object has no attribute ‘next’
后来查阅资料后知道了原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为next了,next是python3.x以前版本中的方法
修改为下面这样运行正常
#-*- coding:utf-8 -*-import timedef A(): while True: print('----A----') yield time.sleep(0.5)def B(c): print('----B----') c.__next__() time.sleep(0.5)if __name__ == '__main__': a = A() print(a.__next__()) B(a)
阅读全文
0 0
- Python3.6 下yield的常见错误(AttributeError: 'generator' object has no attribute 'next')
- Python3下机器学习实战KNN代码出现AttributeError: ‘dict’ object has no attribute错误
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- python 3.2 错误 ‘generator’ object has no attribute ‘next’
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- python 3.x 错误 ‘generator’ object has no attribute ‘next’
- Python3下AttributeError: 'dict' object has no attribute 'iteritems'的问题分析
- Python3下AttributeError: 'dict' object has no attribute 'iteritems'的问题分析
- AttributeError: 'NoneType' object has no attribute 'execute' 错误的解决
- AttributeError: '_csv.reader' object has no attribute 'next'
- AttributeError: '_csv.reader' object has no attribute 'next'
- python3 报错: AttributeError: 'module' object has no attribute 'urlopen'
- python “AttributeError: '_csv.reader' object has no attribute'next'” 报错的解决办法
- [python错误]builtins.AttributeError: 'module' object has no attribute 'request'
- python 错误解析 AttributeError: 'module' object has no attribute 'dumps'
- 出现错误如下:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'
- Python 错误:AttributeError: 'module' object has no attribute
- Django错误:AttributeError: 'WSGIRequest' object has no attribute 'Post'
- Js学习之路二 --- 初识函数
- Cobbler部署指南之安装篇
- java进程占用cpu高
- C++ 学习心得笔记(一)
- react(一) 基础组件封装
- Python3.6 下yield的常见错误(AttributeError: 'generator' object has no attribute 'next')
- 再看屏幕适配
- C++学习笔记,关于一个文件中的全局变量在其他文件中的使用
- shell转义,单引号与双引号,反撇号
- MySql解析顺序
- SSL
- React 修改端口号
- 怎么把gradle项目转为maven项目?
- iOS OpenGL renderbufferStorage:fromDrawable 方法详解