分享

Python3引入什么新的东西?

 码农9527 2022-01-06

Python 3.x引入一些Python2不兼容的关键字和函数,可以通过在 Python2 内置的模块 __future__ 导入。建议如果你想在代码中支持 Python3.x,使用__future__导入它。

Python3引入什么新的东西?

  例如,如果想在 Python2 中拥有 Python 3.x 整数的除法行为,添加下面的 import 语句

  from __future__ import division

  print函数

  在 Python3 最值得注意和最广为人知的变化是print函数的使用。print 函数使用的括号()在Python3中是强制性的。它在 Python2 中是可选的。

print "Hello World" #is acceptable in Python 2print ("Hello World") # in Python 3, print must be followed by () 12复制代码类型:[python]

  print()函数默认情况下在结束时会插入一个换行。在 Python2.它可以通过 ',' 在末行抑制输出换行。 在 Python3 则使用"end=' '" 附加空格,而不是换行

print x,  # Trailing comma suppresses newline in Python 2print(x, end=" ")  # Appends a space instead of a newline in Python 312复制代码类型:[python]

  从键盘读取输入

  Python2 中有输入函数两个版本。 input() 和 raw_input()。如果它被包含在引号 '' 或 "",input() 对待接收到的数据作为字符串,否则数据将被视为数字类型。

  在 Python3 中 raw_input()函数已被弃用。此外,接收到的输入数据总是作为字符串处理。

In Python 2 >>> x=input('something:') 
something:10 #entered data is treated as number>>> x10>>> 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 NameErrorTraceback (most recent call last):
  File "", line 1, in  x=raw_input("something:")
NameError: name 'raw_input' is not defined 123456789101112131415161718192021222324252627复制代码类型:[python]

  整数除法

  在Python2.两个整数的除法的结果会四舍五入到最接近的整数。如:3/2 其结果将显示 1. 为了获得一个浮点除法,分子或分母必须明确为浮点数。因此无论是 3.0/2 或 3/2.0 或 3.0/2.0 将产生1.5 。

  Python3 计算 3/2 默认结果值为 1.5.这对新手程序员更加直观。

  Unicode表示

  Python2 里如果你想将它保存为 Unicode,需要标记为 U 的字符串。

  Python3 中的字符串默认存储为 Unicode。在Python3.我们有个Unicode(UTF-8)字符串和 2 字节类:字节和字节数组。

  xrange()函数已被删除

  在 Python2 的 range() 函数返回一个列表,还有 xrange()返回一个对象只会在需要时在范围内产生所需项目以节省内存。

  在Python3.range()函数去除了,而 xrange()已更名为 range()。 另外在 Python3.2 以及更高的版本中, range()对象支持切片。

  引发异常

  Python2 中同时接受符号的'大胆'和'新'的语法;如果我们不在括号中括入异常参数,Python3 中会引发一个 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 31234复制代码类型:[python]

  异常的参数

  在 Python3.异常参数应以 'as' 关键字来声明。

except Myerror, err: # In Python2except Myerror as err: #In Python 312复制代码类型:[python]

  next() 函数和.next()方法

  在Python 2.next() 作为生成器对象的一个方法是允许的。在 Python2.next()函数过度产生器对象遍历也是可以接受的。在Python3.但是,next()函数作为生成器方法来中止并引发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 312345复制代码类型:[python]

  2to3实用工具

  随着 Python3 解释器,2t03.py 脚本将被通常安装在 tools/scripts 文件夹。 它读取 Python2.x 源代码,并应用了一系列的修复将它转变成有效的 Python3.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)

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多