虽然很多CG制作软件还用着Python2.7(除了开源的Blender用上了3.5),但我还是决定转向Python 3了。今天把两个版本主要的一些区别放在这里:
- print函数
- 整数相除
- Unicode
- 异常处理
- xrange
- map函数
- 不支持has_key
print函数
Python 2中print是语句(statement),Python 3中print则变成了函数。在Python 3中调用print需要加上括号,不加括号会报SyntaxError
Python 2
print "hello world" // hello world
Python 3
print("hello world") // hello world print "hello world" // File "<stdin>", line 1 // print "hello world" // ^ // SyntaxError: Missing parentheses in call to 'print'
整数相除
在Python 2中,3/2的结果是整数,在Python 3中,结果则是浮点数。
Python 2
print '3 / 2 =', 3 / 2 print '3 / 2.0 =', 3 / 2.0 // 3 / 2 = 1 // 3 / 2.0 = 1.5
Python 3
print('3 / 2 =', 3 / 2) print('3 / 2.0 =', 3 / 2.0) // 3 / 2 = 1.5 // 3 / 2.0 = 1.5
这里补充一下,Python3中整除可以使用//
。
Unicode
Python 2有两种字符串类型:str和unicode,Python 3中的字符串默认就是Unicode,Python 3中的str相当于Python 2中的unicode。
在Python 2中,如果代码中包含非英文字符,需要在代码文件的最开始声明编码,如下
# -*- coding: utf-8 -*-
在Python 3中,默认的字符串就是Unicode,就省去了这个麻烦,下面的代码在Python 3可以正常地运行。
a = "你好" print(a)
这个可以参考之前的博文关于字符编码。
异常处理
Python 2中捕获异常一般用下面的语法
try: 1/0 except ZeroDivisionError, e: print str(e) # 或者 try: 1/0 except ZeroDivisionError as e: print str(e)
Python 3中不再支持前一种语法,必须使用as关键字。
xrange
Python 2中有 range 和 xrange 两个方法。
其区别在于,range返回一个list,在被调用的时候即返回整个序列;xrange返回一个iterator,在每次循环中生成序列的下一个数字。
Python 3中不再支持 xrange 方法,Python 3中的 range 方法就相当于 Python 2中的 xrange 方法。
map函数
在Python 2中,map函数返回list,而在Python 3中,map函数返回iterator。
Python 2
map(lambda x: x+1, range(5)) // [1, 2, 3, 4, 5]
Python 3
map(lambda x: x+1, range(5)) // <map object at 0x7ff5b103d2b0> list(map(lambda x: x+1, range(5))) // [1, 2, 3, 4, 5]
filter函数在Python 2和Python 3中也是同样的区别。
不支持has_key
Python 3中的字典不再支持has_key方法
Python 2
person = {"age": 30, "name": "Xiao Wang"} print "person has key \"age\": ", person.has_key("age") print "person has key \"age\": ", "age" in person // person has key "age": True // person has key "age": True
Python 3
person = {"age": 30, "name": "Xiao Wang"} print("person has key \"age\": ", "age" in person) // person has key "age": True print("person has key \"age\": ", person.has_key("age")) // Traceback (most recent call last): // File "<stdin>", line 1, in <module> // AttributeError: 'dict' object has no attribute 'has_key'
以上转载自知乎强哥。
即使是马戏团的狮子,
也会因为怕鞭打而学会坐在椅子上,
但这是“良好的训练”,
而不是“良好的教育”。
《三傻大闹宝莱坞》
——拉库马·希拉尼
评论
266978 454232Why didnt I think about this? I hear exactly what youre saying and Im so pleased that I came across your weblog. You actually know what youre talking about, and you made me feel like I need to learn a lot more about this. Thanks for this; Im officially a huge fan of your blog 992318
561750 25333I genuinely dont accept this specific post. Nonetheless, I had searched with Google and Ive discovered out that you are correct and I had been thinking inside the improper way. Maintain on creating top quality material related to this. 571928
516647 225034The vacation unique deals offered are believed as a selection of possibly the most preferred and therefore within your budget all more than the globe. Quite several hostels can be proudly located inside property which is accented who has striking seashores encouraging crystal-clear rivers, contingency of an Ocean. hotels compare rates 977510
695770 497114A truly interesting examine, I may possibly not concur completely, but you do make some extremely valid points. 696965