+
95
-

回答

Python 2于2000年10月16日发布,稳定版本是Python 2.7。Python 3于2008年12月3日发布,不完全兼容Python 2。

python2 的代码混乱,重复较多,冗余。

python3源码规范、清晰、简单优美。

两者废弃类差异:

1. print 语句被 Python3 废弃,统一使用 print 函数 ;

2. exec 语句被 python3 废弃,统一使用 exec 函数 ;

3. execfile 语句被 Python3 废弃,推荐使用 exec(open("./filename").read()) ;

4. 不相等操作符"<>"被 Python3 废弃,统一使用"!=" ;

5. long 整数类型被 Python3 废弃,统一使用 int ;

6. xrange 函数被 Python3 废弃,统一使用 range,Python3 中 range 的机制也进行修改

并提高了大数据集生成效率 ;

7. Python3 中这些方法再不再返回 list 对象:dictionary 关联的 keys()、values()、items(),

zip(),map(),filter(),但是可以通过 list 强行转换:

(1) mydict={"a":1,"b":2,"c":3} ;

(2)mydict.keys() #<built-in method keys of dict object at 0x000000000040B4C8> ;

(3) list(mydict.keys()) #['a', 'c', 'b'] .

8. 迭代器 iterator 的 next()函数被 Python3 废弃,统一使用 next(iterator) ;

9. raw_input 函数被 Python3 废弃,统一使用 input 函数 ;

10. 字典变量的 has_key 函数被 Python 废弃,统一使用 in 关键词 ;

11. file 函数被 Python3 废弃,统一使用 open 来处理文件,可以通过 io.IOBase 检查

文件类型 ;

12. apply 函数被 Python3 废弃 ;

13. 异常 StandardError 被 Python3 废弃,统一使用 Exception .

网友回复

我知道答案,我要回答