Python 3更新了什么?
Python 3更新了什么?
__future__模块
Python 3.x引入了一些Python 2不兼容的关键字和功能,这些关键字和功能可以通过Python 2中内置的__future__模块导入。如果您正在计划Python 3.x对代码的支持,建议使用__future__ imports。
例如,如果我们想要Python 3.x在Python 2中的整数除法行为,请添加以下import语句。
from __future__ import division
print函数
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()。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' #eentered 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要求您用u标记字符串,如果您想将其存储为Unicode。
python 3默认情况下将字符串存储为Unicode。我们有Unicode (utf-8)字符串和两个字节类:字节和字节数组。
移除xrange()函数
Python 2 range ()返回一个列表,xrange ( )返回一个对象,该对象仅在需要时生成该范围内的项目,从而节省内存。
在Python 3中,range()函数被删除,xrange()被重命名为rang ()。此外,range()对象支持Python 3.2和更高版本中的切片。
引发异常
python 2接受“old”和“new”语法这两种符号;python 3如果不将异常参数括在圆括号中,则会引发语法错误。
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中,next()作为生成器方法被中断并引发属性错误。
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脚本通常安装在“工具/脚本”文件夹中。它读取Python 2.x源代码,并应用一系列修复程序将其转换为有效的Python 3.x代码。
下面是Python 2代码示例(area.py) :
def area(x,y = 3.14): a = y*x*x print a return a a = area(10) print "area",a
转化为Python 3 版本
$2to3 -w area.py
转换后的代码:
def area(x,y = 3.14): # formal parameters a = y*x*x print (a) return a a = area(10) print("area",a)