__future__模块
Python 3.x介绍了一些Python 2不兼容的关键字和功能,可以通过Python 2中内置的__future__模块导入
.如果您计划对代码进行Python 3.x支持,建议使用__future__ imports.
例如,如果我们想在Python 2中使用Python 3.x的整数除法行为,请添加以下导入语句.
from __future__ import division
打印功能
Python 3中最值得注意和最广为人知的变化是如何使用 print 函数.现在必须使用带有打印功能的括号().它在Python 2中是可选的.
print "Hello World" #is acceptable in Python 2print ("Hello World") # in Python 3, print must be followed by ()
print()函数默认在末尾插入一个新行.在Python 2中,可以通过在末尾添加","来抑制它.在Python 3中,"end =''"附加空格而不是换行符.
print x, # Trailing comma suppresses newline in Python 2print(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>>> x10>>> 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 NameErrorTraceback (most recent call last): File "", line 1, in 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()函数已删除
在Python 2中range()返回一个列表,xrange()返回一个对象,该对象只在需要时生成范围内的项目,从而节省内存.
在Python 3中,range()函数是已删除,xrange()已重命名为range().此外,range()对象支持Python 3.2及更高版本中的切片.
引发异常
Python 2接受两种符号,'old'和'新'语法;如果我们不在括号中包含异常参数,Python 3会引发一个SyntaxError.
raise IOError, "file error" #This is accepted in Python 2raise IOError("file error") #This is also accepted in Python 2raise IOError, "file error" #syntax error is raised in Python 3raise IOError("file error") #this is the recommended syntax in Python 3
异常中的参数
在Python 3中,异常的参数应该用'as'关键字声明.
except Myerror, err: # In Python2except Myerror as err: #In Python 3
next()函数和.next()方法
在Python 2中,next()作为方法允许生成器对象.在Python 2中,也接受了迭代生成器对象的next()函数.然而,在Python 3中,next(0作为生成器方法停止并引发 AttributeError .
gen = (letter for letter in 'Hello World') # creates generator objectnext(my_generator) #allowed in Python 2 and Python 3my_generator.next() #allowed in Python 2. raises AttributeError in Python 3
2to3 Utility
除了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 aa = area(10)print "area",aTo convert into Python 3 version:$2to3 -w area.pyConverted code :def area(x,y = 3.14): # formal parameters a = y*x*x print (a) return aa = area(10)print("area",a)