您的位置:首页 > 编程语言 > Python开发

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: