Python 2.x和3.x不同点
2015-07-01 15:32
561 查看
1.print和print()
2.yield
出现下面的错误
Traceback (most recent call last):
File “<pyshell#32>”, line 1, in <module>
f.next()
AttributeError: ‘generator’ object has no attribute ‘next’
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
3.unicode
在Python3.x中, 没有预定义unicode类型了,内置字符串就是str, 但是str中的字符都是unicode编码的
4.cmp()
在Python3.x中开始没这个函数了,官方文档是这么写的
The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
大意就是cmp()函数已经“离开”了,如果你真的需要cmp()函数,你可以用表达式(a > b) - (a < b)代替cmp(a,b)
2.yield
出现下面的错误
Traceback (most recent call last):
File “<pyshell#32>”, line 1, in <module>
f.next()
AttributeError: ‘generator’ object has no attribute ‘next’
原因是在python 3.x中 generator(有yield关键字的函数则会被识别为generator函数)中的next变为__next__了,next是python 3.x以前版本中的方法
3.unicode
在Python3.x中, 没有预定义unicode类型了,内置字符串就是str, 但是str中的字符都是unicode编码的
4.cmp()
在Python3.x中开始没这个函数了,官方文档是这么写的
The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)
大意就是cmp()函数已经“离开”了,如果你真的需要cmp()函数,你可以用表达式(a > b) - (a < b)代替cmp(a,b)
相关文章推荐
- python enumerate函数用法
- Core Python Notes
- 《用Python进行自然语言处理》归纳二
- Python序列化
- python IDE使用技巧
- Python 类总结
- python出现ImportError: No module named site
- python 国内镜像
- python脚本发送邮件
- [RabbitMQ+Python入门经典] 兔子和兔子窝
- Python 添加模块
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or
- 【转载】python 中的 and、or、and-or