首页 > 编程语言 > 详细

Python 2.x和3.x不同点

时间:2015-07-01 17:17:21      阅读:197      评论:0      收藏:0      [点我收藏+]

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)

Python 2.x和3.x不同点

原文:http://www.cnblogs.com/edisonxiang/p/4613380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!