
正文
Python:Python 3.x 的革新
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Python 3.x 版本在设计时为了向最好的语言前进,没有考虑向下兼容,许多针对早期 Python 版本设计的程序都无法正常运行。本文简单介绍了 Python 3.x 版本较之 2.x 版本语法上的一些变化。
一、文件编码
默认源文件编码从 ASCII 变为 UTF-8,从而支持非 ASCII 字符标识符,这使得以下代码合法:
中国 = 'china'
print(中国)
二、函数
-
去除
print
语句,加入
print()
函数实现相同的功能;
-
exec
语句也已改为
exec()
函数;
-
删除了
raw_input()
,统一使用
input()
;
-
xrange()
改名为
range()
,要想使用
range()
获得一个 list,必须显式调用:
list(range(10))
;
-
函数
zip()
、
map()
和
filter()
将返回迭代器 ;
-
去除了
apply()
、
callable()
、
coerce()
、
execfile()
和
reload()
函数;
-
reduce()
移动到了 functools 模块;
-
添加
repr()
函数实现 2.x 版本中反引号的功能;
-
修改了
oct()
函数,增加了
bin()
函数;
-
元组参数解包使用方法变为:
def func(*z, **p): pass
;
-
dict 的
.keys()
、
.items()
和
.values()
方法返回迭代器,
iterkeys()
、
dict.has_key()
被废弃,可用
in
替代;
1、print 函数语法区别
print("行末不换行", end=" ")
print >>sys.stderr, "fatal error" # 2.x 流转向
print("fatal error", file=sys.stderr) # 3.x 流转向
2、字符串格式化变化
>>> "I love {0}, {1}, and {param}".format("eggs", "bacon", param="sausage")
'I love eggs, bacon, and sausage'
三、数学运算
-
除法成为了真正的除法,也即 5/2 结果为 2.5;
-
取整的除法使用
//
,也即 5//2 结果为 2;
-
不等号只能用
!=
;
-
如果
x < y
不能比较,抛出 TypeError 异常而非返回伪随机布尔值;
四、数据类型
-
现在字符串只有 str 一种类型,但与 2.x 版本的 unicode 基本一致;
-
去除了 long 类型,保留整型 int——实际相当于 2.x 版本的 long 类型;
-
新增 bytes 类型。
五、其它细节
-
扩展的可迭代解包:
a, b, *rest = seq
和
*rest, a = seq
,只要 rest 为 list 并且 seq 可迭代;
-
关键词加入
as
、
with
、
True
、
False
、
None
;
-
加入 nonlocal 语句。使用
noclocal x
可以直接指派外部(非全局)变量。
六、面向对象
-
引入抽象基类(Abstraact Base Classes,ABCs)并增加了
@abstractmethod
和
@abstractproperty
两个 decorator 使抽象方法(属性)编写更加方便;
-
废弃了 file 类;
-
迭代器的
next()
方法改名为
__next__()
,并增加内置函数
next()
,用以调用迭代器的
__next__()
方法 ;
七、异常
-
抛出异常:使用
raise Exception(args)
而非
raise Exception, args
;
-
捕获异常:使用
except Exception as identifier
而非
except Exception, identifier
;
八、模块变动
-
移除了cPickle、imageop、audiodev、Bastion、bsddb185、exceptions、linuxaudiodev、md5、MimeWriter、mimify、popen2、rexec、sets、sha、stringold、strop、sunaudiodev、timing、xmllib、bsddb 以及 new 模块。
-
模块名称变化:ConfigParser → configparser、Tkinter → tkinter 、ttk → tkinter.ttk 、tkMessageBox → tkinter.messagebox、 tkColorChooser → tkinter.colorchooser、 tkFileDialog → tkinter.filedialog、 tkCommonDialog → tkinter.commondialog、 tkSimpleDialog → tkinter.simpledialog、 tkFont → tkinter.font、
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。
-
更多变化请参阅:The Python 3.4 Standard Library
print
语句,加入
print()
函数实现相同的功能;
exec
语句也已改为
exec()
函数;
raw_input()
,统一使用
input()
;
xrange()
改名为
range()
,要想使用
range()
获得一个 list,必须显式调用:
list(range(10))
;
zip()
、
map()
和
filter()
将返回迭代器 ;
apply()
、
callable()
、
coerce()
、
execfile()
和
reload()
函数;
reduce()
移动到了 functools 模块;
repr()
函数实现 2.x 版本中反引号的功能;
oct()
函数,增加了
bin()
函数;
def func(*z, **p): pass
;
.keys()
、
.items()
和
.values()
方法返回迭代器,
iterkeys()
、
dict.has_key()
被废弃,可用
in
替代;
print("行末不换行", end=" ")
print >>sys.stderr, "fatal error" # 2.x 流转向
print("fatal error", file=sys.stderr) # 3.x 流转向
2、字符串格式化变化
>>> "I love {0}, {1}, and {param}".format("eggs", "bacon", param="sausage")
'I love eggs, bacon, and sausage'
三、数学运算
-
除法成为了真正的除法,也即 5/2 结果为 2.5;
-
取整的除法使用
//
,也即 5//2 结果为 2;
-
不等号只能用
!=
;
-
如果
x < y
不能比较,抛出 TypeError 异常而非返回伪随机布尔值;
四、数据类型
-
现在字符串只有 str 一种类型,但与 2.x 版本的 unicode 基本一致;
-
去除了 long 类型,保留整型 int——实际相当于 2.x 版本的 long 类型;
-
新增 bytes 类型。
五、其它细节
-
扩展的可迭代解包:
a, b, *rest = seq
和
*rest, a = seq
,只要 rest 为 list 并且 seq 可迭代;
-
关键词加入
as
、
with
、
True
、
False
、
None
;
-
加入 nonlocal 语句。使用
noclocal x
可以直接指派外部(非全局)变量。
六、面向对象
-
引入抽象基类(Abstraact Base Classes,ABCs)并增加了
@abstractmethod
和
@abstractproperty
两个 decorator 使抽象方法(属性)编写更加方便;
-
废弃了 file 类;
-
迭代器的
next()
方法改名为
__next__()
,并增加内置函数
next()
,用以调用迭代器的
__next__()
方法 ;
七、异常
-
抛出异常:使用
raise Exception(args)
而非
raise Exception, args
;
-
捕获异常:使用
except Exception as identifier
而非
except Exception, identifier
;
八、模块变动
-
移除了cPickle、imageop、audiodev、Bastion、bsddb185、exceptions、linuxaudiodev、md5、MimeWriter、mimify、popen2、rexec、sets、sha、stringold、strop、sunaudiodev、timing、xmllib、bsddb 以及 new 模块。
-
模块名称变化:ConfigParser → configparser、Tkinter → tkinter 、ttk → tkinter.ttk 、tkMessageBox → tkinter.messagebox、 tkColorChooser → tkinter.colorchooser、 tkFileDialog → tkinter.filedialog、 tkCommonDialog → tkinter.commondialog、 tkSimpleDialog → tkinter.simpledialog、 tkFont → tkinter.font、
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。
-
更多变化请参阅:The Python 3.4 Standard Library
>>> "I love {0}, {1}, and {param}".format("eggs", "bacon", param="sausage")
'I love eggs, bacon, and sausage'
- 除法成为了真正的除法,也即 5/2 结果为 2.5;
-
取整的除法使用
//,也即 5//2 结果为 2; -
不等号只能用
!=; -
如果
x < y不能比较,抛出 TypeError 异常而非返回伪随机布尔值;
四、数据类型
-
现在字符串只有 str 一种类型,但与 2.x 版本的 unicode 基本一致;
-
去除了 long 类型,保留整型 int——实际相当于 2.x 版本的 long 类型;
-
新增 bytes 类型。
五、其它细节
-
扩展的可迭代解包:
a, b, *rest = seq
和
*rest, a = seq
,只要 rest 为 list 并且 seq 可迭代;
-
关键词加入
as
、
with
、
True
、
False
、
None
;
-
加入 nonlocal 语句。使用
noclocal x
可以直接指派外部(非全局)变量。
六、面向对象
-
引入抽象基类(Abstraact Base Classes,ABCs)并增加了
@abstractmethod
和
@abstractproperty
两个 decorator 使抽象方法(属性)编写更加方便;
-
废弃了 file 类;
-
迭代器的
next()
方法改名为
__next__()
,并增加内置函数
next()
,用以调用迭代器的
__next__()
方法 ;
七、异常
-
抛出异常:使用
raise Exception(args)
而非
raise Exception, args
;
-
捕获异常:使用
except Exception as identifier
而非
except Exception, identifier
;
八、模块变动
-
移除了cPickle、imageop、audiodev、Bastion、bsddb185、exceptions、linuxaudiodev、md5、MimeWriter、mimify、popen2、rexec、sets、sha、stringold、strop、sunaudiodev、timing、xmllib、bsddb 以及 new 模块。
-
模块名称变化:ConfigParser → configparser、Tkinter → tkinter 、ttk → tkinter.ttk 、tkMessageBox → tkinter.messagebox、 tkColorChooser → tkinter.colorchooser、 tkFileDialog → tkinter.filedialog、 tkCommonDialog → tkinter.commondialog、 tkSimpleDialog → tkinter.simpledialog、 tkFont → tkinter.font、
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。
-
更多变化请参阅:The Python 3.4 Standard Library
-
扩展的可迭代解包:
a, b, *rest = seq和*rest, a = seq,只要 rest 为 list 并且 seq 可迭代; -
关键词加入
as、with、True、False、None; -
加入 nonlocal 语句。使用
noclocal x可以直接指派外部(非全局)变量。
六、面向对象
-
引入抽象基类(Abstraact Base Classes,ABCs)并增加了
@abstractmethod
和
@abstractproperty
两个 decorator 使抽象方法(属性)编写更加方便;
-
废弃了 file 类;
-
迭代器的
next()
方法改名为
__next__()
,并增加内置函数
next()
,用以调用迭代器的
__next__()
方法 ;
七、异常
-
抛出异常:使用
raise Exception(args)
而非
raise Exception, args
;
-
捕获异常:使用
except Exception as identifier
而非
except Exception, identifier
;
八、模块变动
-
移除了cPickle、imageop、audiodev、Bastion、bsddb185、exceptions、linuxaudiodev、md5、MimeWriter、mimify、popen2、rexec、sets、sha、stringold、strop、sunaudiodev、timing、xmllib、bsddb 以及 new 模块。
-
模块名称变化:ConfigParser → configparser、Tkinter → tkinter 、ttk → tkinter.ttk 、tkMessageBox → tkinter.messagebox、 tkColorChooser → tkinter.colorchooser、 tkFileDialog → tkinter.filedialog、 tkCommonDialog → tkinter.commondialog、 tkSimpleDialog → tkinter.simpledialog、 tkFont → tkinter.font、
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。
-
更多变化请参阅:The Python 3.4 Standard Library
@abstractmethod
和
@abstractproperty
两个 decorator 使抽象方法(属性)编写更加方便;
next()
方法改名为
__next__()
,并增加内置函数
next()
,用以调用迭代器的
__next__()
方法 ;
-
抛出异常:使用
raise Exception(args)而非raise Exception, args; -
捕获异常:使用
except Exception as identifier而非except Exception, identifier;
八、模块变动
-
移除了cPickle、imageop、audiodev、Bastion、bsddb185、exceptions、linuxaudiodev、md5、MimeWriter、mimify、popen2、rexec、sets、sha、stringold、strop、sunaudiodev、timing、xmllib、bsddb 以及 new 模块。
-
模块名称变化:ConfigParser → configparser、Tkinter → tkinter 、ttk → tkinter.ttk 、tkMessageBox → tkinter.messagebox、 tkColorChooser → tkinter.colorchooser、 tkFileDialog → tkinter.filedialog、 tkCommonDialog → tkinter.commondialog、 tkSimpleDialog → tkinter.simpledialog、 tkFont → tkinter.font、
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。
-
更多变化请参阅:The Python 3.4 Standard Library
Tkdnd → tkinter.dnd、 ScrolledText → tkinter.scrolledtext 以及 Tix → tkinter.tix。








