Python 3中的新功能(What is New in Python 3)
__future__模块
Python 3.x引入了一些Python 2不兼容的关键字和功能,可以通过Python 2中的内置__future__模块导入。如果您计划对代码进行Python 3.x支持,建议使用__future__导入。
例如,如果我们想在Python 2中使用Python 3.x的整数除法行为,请添加以下import语句。
from __future__ import division
打印功能
Python 3中最值得注意和最广为人知的变化是如何使用print功能。 现在必须使用带有打印功能的括号()。 它在Python 2中是可选的。
print "Hello World" #is acceptable in Python 2
print ("Hello World") # in Python 3, print must be followed by ()
默认情况下,print()函数在末尾插入一个新行。 在Python 2中,可以通过在末尾添加“,”来抑制它。 在Python 3中,“end =''”追加空格而不是换行符。
print x, # Trailing comma suppresses newline in Python 2
print(x, end=" ") # Appends a space instead of a newline in Python 3
从键盘读取输入
Python 2有两个版本的输入函数, input()和raw_input() 。 如果输入()函数包含在引号''或“”中,则它将接收的数据视为字符串,否则数据将被视为数字。
在Python 3中,不推荐使用raw_input()函数。 此外,接收的数据始终被视为字符串。
In Python 2
>>> x = input('something:')
something:10 #entered data is treated as number
>>> x
10
>>> x = input('something:')
something:'10' #entered data is treated as string
>>> x
'10'
>>> x = raw_input("something:")
something:10 #entered data is treated as string even without ''
>>> x
'10'
>>> x = raw_input("something:")
something:'10' #entered data treated as string including ''
>>> x
"'10'"
In Python 3
>>> x = input("something:")
something:10
>>> x
'10'
>>> x = input("something:")
something:'10' #entered data treated as string with or without ''
>>> x
"'10'"
>>> x = raw_input("something:") # will result NameError
Traceback (most recent call last):
File "<pyshell#3>", line 1, in
<module>
x = raw_input("something:")
NameError: name 'raw_input' is not defined
整数部
在Python 2中,两个整数除法的结果四舍五入到最接近的整数。 结果,3/2将显示1.为了获得浮点除法,必须明确地将分子或分母用作浮点数。 因此,3.0/2或3/2.0或3.0/2.0将导致1.5
Python 3默认将3/2评估为1.5,这对于新程序员来说更直观。
Unicode表示
Python 2要求您使用au标记字符串,如果要将其存储为Unicode。
默认情况下,Python 3将字符串存储为Unicode。 我们有Unicode(utf-8)字符串和2字节类:字节和字节数组。
xrange() Function Removed
在Python 2中,range()返回一个列表,xrange()返回一个对象,该对象只在需要时生成范围内的项目,从而节省内存。
在Python 3中,删除了range()函数,并且xrange()已重命名为range()。 此外,range()对象支持Python 3.2及更高版本中的切片。
提出例外
Python 2接受两种符号,“旧”和“新”语法; 如果我们不在括号中包含异常参数,Python 3会引发SyntaxError。
raise IOError, "file error" #This is accepted in Python 2
raise IOError("file error") #This is also accepted in Python 2
raise IOError, "file error" #syntax error is raised in Python 3
raise IOError("file error") #this is the recommended syntax in Python 3
异常中的参数
在Python 3中,异常的参数应该用'as'关键字声明。
except Myerror, err: # In Python2
except Myerror as err: #In Python 3
next()函数和.next()方法
在Python 2中,允许next()作为生成器对象的方法。 在Python 2中,也接受了迭代生成器对象的next()函数。 但是,在Python 3中,下一步(0作为生成器方法已停止并引发AttributeError 。
gen = (letter for letter in 'Hello World') # creates generator object
next(my_generator) #allowed in Python 2 and Python 3
my_generator.next() #allowed in Python 2. raises AttributeError in Python 3
2to3实用程序
与Python 3解释器一起,2to3.py脚本通常安装在tools/scripts文件夹中。 它读取Python 2.x源代码并应用一系列修复程序将其转换为有效的Python 3.x代码。
Here is a sample Python 2 code (area.py):
def area(x,y = 3.14):
a = y*x*x
print a
return a
a = area(10)
print "area",a
To convert into Python 3 version:
$2to3 -w area.py
Converted code :
def area(x,y = 3.14): # formal parameters
a = y*x*x
print (a)
return a
a = area(10)
print("area",a)